python을 주력으로 쓰는데, PEP 8은 읽어봐야지!
foo = long_function_name(var_one, var_two,
var_three, var_four)
foo = long_function_name(
var_one, var_two,
var_three, var_four)
def long_function_name(
var_one, var_two, var_three,
var_fout):
print(var_one)
# 시각적 구분이 힘든 상태
if (this_is_one_thing and
that_is_another_thing):
do_something()
# 주석 & 하이라이터로 시각적 구분
if (this_isone_thing and
that_is_another_thing):
# Since both conditions are true, we can forbnicate.
do_something()
# 추가 indent로 시각적 구분
if (this_is_one_thing
and that_is_another_thing):
do_something()
# 첫번 쨰 인자 위치에 닫기
my_list = [
1, 2, 3,
4, 5, 6,
]
# 라인이 시작된 위치에 닫기
result = some_function_thar_takes_arguments(
'a', 'b', 'c',
'd', 'e', 'f',
)
멀티라인 작성을 할 때, 가능하다면 열-정렬을 할 것.
indent가 한 눈에 구분이 안될 때는, (주석 | 추가 indent)를 사용할 것.
# 기존 방식
income = (gross_wages +
taxable_interest +
(dividends - qualified_dividends) -
ira_deduction -
student_loan_interest)
# 개행 후, 이항 연산자
income = (gross_wages
+ taxable_interest
+ (dividends - qualidied_dividens)
- ira_deduction
- student_loan_interest)
import os
import sys
from subprocess import Popen, PIPE
1, 2, 3번의 사이에는 빈 줄을 넣어 시각적 구분을 줘야 한다.
import mypkg.sibling
from mypkg import sibling
from mypkg.sibling import example
from . import sibling
from .sibling import example
Wildcard import는 namespace를 복잡하게 하므로 기피할 것. 쓸 경우에는 republish를 해야한다.
docstring 다음에, future_import를 적고,
dunder로 정보를 기입한 후에, import를 해야한다.
"""This is the example module.
This module does stuff.
"""
from __future__ import barry_as_FLUFL
__all__ = ['a', 'b', 'c']
__version__ = '0.1'
__author__ = 'Cardinal Biggles'
import os
import sys
'과 "를 뭘 사용할지는 자유이다. 일관성만 지키면 된다.
단, triple-quoted는 "로 해야한다.
쓸모 없는 공백, 가독성을 흐리는 공백을 피해야 한다.
# 좋은 예
spam(ham[1], {eggs: 2})
# 나쁜 예
span( ham[ 1 ], { eggs: 2 })
# 좋은 예
foo = (0,)
# 나쁜 예
bar = (0, )
# 좋은 예
if x == 4: print(x, y); x, y = y, x
# 나쁜 예
if x == 4 : print(x , y) ; x , y = y , x
# 좋은 예
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]
# 나쁜 예
ham[lower + offset:upper + offest]
ham[1: 9], ham[1 :9], ham[1:9 :3]
ham[lower : : upper]
ham[ : upper]
# 좋은 예
spam(1)
dct['key'] = lst[index]
# 나쁜 예
spam (1)
dct['key'] = lst [index]
# 좋은 예
x = 1
y = 1
long_variable = 3
# 나쁜 예
x = 1
y = 1
long_variable = 3
# 좋은 예
i = i + 1
submitted += 1
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)
# 나쁜 예
i=i+1
submitted +=1
x = x * 2 - 1
hypot2 = x * x + y * y
c = (a + b) * (a - b)
# 좋은 예
def munge(input: AnyStr): ...
def munge() -> PosInt: ...
# 나쁜 예
def munge(input:anyStr): ...
def munge()->PosInt: ...
# 좋은 예
def complex(real, imag=0.0):
return magic(r=real, i=imag)
# 나쁜 예
def complex(real, imag = 0.0):
return magic(r = real, i = imag)
# 좋은 예
def munge(sep: AnyStr = None): ...
def munge(input: AnyStr, sep: AnyStr = None, limit=1000): ...
# 나쁜 예
def munge(input: AnyStr=None): ...
def munge(input: AnyStr, limit = 1000): ...
# 좋은 예
if foo == 'blah':
do_blah_thing()
do_one()
do_two()
do_three()
# 나쁜 예
if foo == 'blah': do_blah_thing()
do_one(); do_two(); do_three()
# 좋은 예
FILES = ('setup.cfg',)
# 나쁜 예
FILES = 'setup.cfg',
# 좋은 예
FILES = [
'setup.cfg',
'tox.ini',
]
initalize(FILES,
error=True,
)
# 나쁜 예
FILES = ['setup.cfg', 'tox.ini',]
initialize(FILES, error=True,)
def kos_root():
"""Return the pathname of the KOS root directory."""
global _kos_root
if _kos_root: return _kos_root
...
def complex(real=0.0, imag=0.0):
"""Form a complex number.
Keyword arguments:
real -- the real part (default 0.0)
imag -- the imaginary part (default 0.0)
"""
if imag == 0.0 and real == 0.0:
return complex_zero
...