Python

[PEP8] Whitespace in Expressions and Statements

ju_young 2022. 2. 7. 13:47
728x90

Pet Peeves

  • 대괄호, 중괄호, 소괄호 안의 공백
# Correct:
spam(ham[1], {eggs: 2})

# Wrong:
spam( ham[ 1 ], { eggs: 2 } )
  • 마지막 쉼표와 닫는 괄호 사이의 공백
# Correct:
foo = (0,)

# Wrong:
bar = (0, )
  • 콤마, 세미클론, 콜론 앞의 공백
# Correct:
if x == 4: print(x, y); x, y = y, x

# Wrong:
if x == 4 : print(x , y) ; x , y = y , x
  • 슬라이스에서는 콜론의 양쪽 간격이 동일(단, 슬라이스의 parameter가 생략되면 공백도 생략)
# Correct:
ham[1:9], ham[1:9:3], ham[:9:3], ham[1::3], ham[1:9:]
ham[lower:upper], ham[lower:upper:], ham[lower::step]
ham[lower+offset : upper+offset]
ham[: upper_fn(x) : step_fn(x)], ham[:: step_fn(x)]
ham[lower + offset : upper + offset]

# Wrong:
ham[lower + offset:upper + offset]
ham[1: 9], ham[1 :9], ham[1:9 :3]
ham[lower : : upper]
ham[ : upper]
  • 함수 호출
# Correct:
spam(1)

# Wrong:
spam (1)
  • indexing 또는 slicing의 여는 괄호 앞의 공백
# Correct:
dct['key'] = lst[index]

# Wrong:
dct ['key'] = lst [index]
  • 할당 operator 주위에 하나 이상의 공백
# Correct:
x = 1
y = 2
long_variable = 3

# Wrong:
x             = 1
y             = 2
long_variable = 3

Other Recommendations

  • 어디에서나 마지막 공백은 피해야한다. 왜냐하면 일반적으로 보이지 않기 때문에 혼동이 될 수 있기 때문이다.
  • 항상 양쪽에 단일 공백 : =, +=, -=, ==, <, >, !=, <>, <=, >=, in, not in, is, is not, and, or, not
  • 우선 순위가 다른 operator를 사용하는 경우 우선 순위가 낮은 연산자 주위에 공백을 추가
# Correct:
i = i + 1
submitted += 1
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)

# Wrong:
i=i+1
submitted +=1
x = x * 2 - 1
hypot2 = x * x + y * y
c = (a + b) * (a - b)
  • 함수 주석은 콜론에 대해 일반적인 규칙을 적용하며 '->'가 있을 경우 주위에 항상 공백이 있어야함
# Correct:
def munge(input: AnyStr): ...
def munge() -> PosInt: ...

# Wrong:
def munge(input:AnyStr): ...
def munge()->PosInt: ...
  • 키워드 argument를 나타내거나 주석이 없는 함수 parameter의 기본값을 나타내는 경우 = 주위에 공백을 사용하지 않음
# Correct:
def complex(real, imag=0.0):
    return magic(r=real, i=imag)
    
# Wrong:
def complex(real, imag = 0.0):
    return magic(r = real, i = imag)

하지만 함수 주석과 기본값을 같이 작성할 경우 공백을 포함

# Correct:
def munge(sep: AnyStr = None): ...
def munge(input: AnyStr, sep: AnyStr = None, limit=1000): ...

# Wrong:
def munge(input: AnyStr=None): ...
def munge(input: AnyStr, limit = 1000): ...
  • Compound 문은 일반적으로 권장되지 않음
# Correct:
if foo == 'blah':
    do_blah_thing()
do_one()
do_two()
do_three()

# Wrong:
if foo == 'blah': do_blah_thing()
do_one(); do_two(); do_three()
  • 때로는 간단한 본문과 같이 if/for/while문을 같은 줄에 작성하는 것이 괜찮지만 다음과 같은 경우는 피해야한다.
# Wrong:
if foo == 'blah': do_blah_thing()
for x in lst: total += x
while t < 10: t = delay()

# Wrong:
if foo == 'blah': do_blah_thing()
else: do_non_blah_thing()

try: something()
finally: cleanup()

do_one(); do_two(); do_three(long, argument,
                             list, like, this)

if foo == 'blah': one(); two(); three()

 

[reference]

https://www.python.org/dev/peps/pep-0008/#whitespace-in-expressions-and-statements

728x90

'Python' 카테고리의 다른 글

[PEP8] Comments  (0) 2022.02.26
[PEP8] When to use trailing commas  (0) 2022.02.25
[PEP8] String Quotes  (0) 2022.02.07
[PEP8] Code Lay-out  (0) 2022.02.04
한 번 사용하고 버려지는 Iterator 객체  (0) 2021.06.13