Python

[PEP8] When to use trailing commas

ju_young 2022. 2. 25. 17:49
728x90

 

  • 하나의 요소로된 튜플을 만들때 마지막에 comma를 추가하고 괄호로 감싸는 것을 추천

 

# Correct:
FILES = ('setup.cfg',)

# Wrong:
FILES = 'setup.cfg',

 

  • trailing comma는 나중에 list, arguments, imported items 등 확장될 것으로 예상될 때 유용
  • 각 line에 trailing comma를 추가하고 괄호는 다음 줄에 닫는다.
  • trailing comma와 같은 줄에 괄호를 닫는 것은 의미가 없음

 

# Correct:
FILES = [
    'setup.cfg',
    'tox.ini',
    ]
initialize(FILES,
           error=True,
           )
           
# Wrong:
FILES = ['setup.cfg', 'tox.ini',]
initialize(FILES, error=True,)

 

[reference]

https://www.python.org/dev/peps/pep-0008/#when-to-use-trailing-commas

728x90

'Python' 카테고리의 다른 글

[PEP8] Comments  (0) 2022.02.26
[PEP8] Whitespace in Expressions and Statements  (0) 2022.02.07
[PEP8] String Quotes  (0) 2022.02.07
[PEP8] Code Lay-out  (0) 2022.02.04
한 번 사용하고 버려지는 Iterator 객체  (0) 2021.06.13