Skip to content

Latest commit

Β 

History

History

Python

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Β 
Β 
Β 
Β 

Python

기본적으둜 PEP8의 μŠ€νƒ€μΌμ„ λ”°λ¦…λ‹ˆλ‹€.

μ½”λ“œ μŠ€νƒ€μΌ 도ꡬ

μΌκ΄€λœ μŠ€νƒ€μΌμ„ μž‘μ„±ν•˜λŠ”λ° 도움을 μ£ΌλŠ” λ‹€μ–‘ν•œ 도ꡬ듀을 ν™œμš©ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

black

μœ„μ˜ μ—¬λŸ¬ 가지 도ꡬ쀑에 κ°€μž₯ 쀑심이 λ˜λŠ” 것은 blackμž…λ‹ˆλ‹€. blackμ—μ„œ κΈ°λ³Έκ°’μœΌλ‘œ μ§€μ •λœ 섀정을 λ”°λ₯΄κ³  μžˆμŠ΅λ‹ˆλ‹€. blackμ—μ„œ λ§Œλ“€μ–΄μ£ΌλŠ” μ½”λ“œμ˜ μŠ€νƒ€μΌμ€ μ΄κ³³μ—μ„œ 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.

isort

black & isort

[tool.isort]
profile = "black"

flake8

black & flake8

max-line-length = 88
extend-ignore = E203

Type Annotation

νƒ€μž… 힌트의 ν™œμš©μ€ μ½”λ“œ μž‘μ„±μž μž¬λŸ‰μ— λ§‘κΉλ‹ˆλ‹€.

Do

def show_movie(name: str) -> None:
    print("Hello " + name)


def show_movie(name: str) -> None:
    if not name:
        return
    print("Hello " + name)

μ΄μ™Έμ˜ κ·œμΉ™

Module Level Dunder

λͺ¨λ“ˆ 레벨의 λ˜λ”(__all__, __version__ λ“±)듀은 λͺ¨λ“ˆ Docstring ν•˜λ‹¨μ— 그리고 일반적인 λͺ¨λ“ˆ μž„ν¬νŠΈ μœ„μ— μœ„μΉ˜ν•˜λ„λ‘ ν•©λ‹ˆλ‹€.

"""Module Docstring Sample

Blah Blah
"""

__all__ = ['A', 'B', 'C']

import sys
import os

Class

클래슀λ₯Ό μ •μ˜ν•  λ•ŒλŠ” μ΅œμƒμœ„ 클래슀 object 상속을 λͺ…μ‹œν•˜μ§€ μ•Šκ³  μƒλž΅ν•©λ‹ˆλ‹€.

Do

class Movie:
    pass

Don't

class Movie(object):
    pass

Comments

Google Python Style Guide의 룰을 λ”°λ¦…λ‹ˆλ‹€ .

def fetch_smalltable_rows(
    table_handle: smalltable.Table,
    keys: Sequence[bytes | str],
    require_all_keys: bool = False,
) -> Mapping[bytes, tuple[str, ...]]:
    """Fetches rows from a Smalltable.

    Retrieves rows pertaining to the given keys from the Table instance
    represented by table_handle.  String keys will be UTF-8 encoded.

    Args:
        table_handle: An open smalltable.Table instance.
        keys: A sequence of strings representing the key of each table
          row to fetch.  String keys will be UTF-8 encoded.
        require_all_keys: If True only rows with values set for all keys will be
          returned.

    Returns:
        A dict mapping keys to the corresponding table row data
        fetched. Each row is represented as a tuple of strings. For
        example:

        {b'Serak': ('Rigel VII', 'Preparer'),
         b'Zim': ('Irk', 'Invader'),
         b'Lrrr': ('Omicron Persei 8', 'Emperor')}

        Returned keys are always bytes.  If a key from the keys argument is
        missing from the dictionary, then that row was not found in the
        table (and require_all_keys must have been False).

    Raises:
        IOError: An error occurred accessing the smalltable.
    """

f-string with equal sign

λ‘œκΉ… λ“±μ˜ λͺ©μ μœΌλ‘œ μ‚¬μš©λ  λ•Œ, ν•„μš”ν•œ 경우 λ“±ν˜Έλ₯Ό ν™œμš©ν•©λ‹ˆλ‹€. λ‹€λ§Œ, λ‹€μŒ κ²½μš°μ— λŒ€ν•΄μ„œλŠ” 직접 ν¬λ§·νŒ…μ„ ν•©λ‹ˆλ‹€.

  • κ΅¬λΆ„μžλ‘œ λ“±ν˜Έκ°€ μ•„λ‹Œ 기호λ₯Ό μ‚¬μš©ν•΄μ•Όν•˜λŠ” 경우
  • ν‰κ°€λ˜λŠ” 식과 λ‹€λ₯Έ λ‚΄μš©μœΌλ‘œ headerλ₯Ό 적어야 ν•  λ•Œ

Do

logger.info(f"{name = }, {description = }")

Don't do

logger.info(f"name = {name!r}, description = {description!r}")

Other case

logger.info(f"name: {name!r}, description: {description!r}") # λ“±ν˜Έ λŒ€μ‹  쌍점 μ‚¬μš©μ΄ ν•„μš”ν•œ 경우
logger.info(f"{a} + {b} = {a + b}")  # 이름이 μ•„λ‹Œ μ‹€μ œ 값이 μ“°μ—¬μ•Ό ν•΄μ„œ {a + b = }둜 λŒ€μ²΄ν•  수 μ—†λŠ” 경우
logger.info(f"result = {a + b}")  # 식(a + b) λŒ€μ‹  λ³„λ„μ˜ 이름이 ν•„μš”ν•œ 경우