-
[Python] PEP - 스타일코드 공백(Whitespace)Python/Python Programming 2018. 5. 14. 22:43
PEP (Python Enhancement Proprosal) : 파이썬 개선 제안서
DOCS : https://www.python.org/dev/peps/pep-0008/
1. 파이썬의 들여쓰기는 공백 2칸, 4탄, 탭(tab)을 모두 허용하지만, 파이썬 코딩 스타일 가이드(PEP 8)에서 '공백 4칸' 을 규정
구 분
설 명
공백 4칸
(PEP 8 규정)
if ['a']:
print('This is True') # 공백 4칸
This is True
공백 2칸
if ['a']:
print('This is True') # 공백 2칸
This is True
탭
if ['a']:
print('This is True') # 탭
his is True
혼용하면 문법에러 발생
if ['a']:print('This is True') # 공백 2칸
print('This is True') # 공백 4칸IndentationError: unexpected indent
2. 불필요한 공백은 쓰지 않는다
구 분
설 명
[ ], { } 감싸여지는 처음과 끝에는 공백없음
O : spam(ham[1], {eggs: 2})
X : spam( ham[ 1 ], { eggs: 2 } )
콤마를 쓰고 바로 ( ), [ ], { } 닫을 때O : foo = (0,) # 한개 원소 튜플 생성
X : bar = (0, )
콤마, 세미콜론, 콜론 전
O : if x == 4: print x, y; x, y = y, x
X : if x == 4 : print x , y ; x , y = y , x
슬라이싱
O : ham[1:9], ham[1:9:3], ham[:9:3], ham[1::3], ham[1:9:]
X : ham[1: 9], ham[1 :9], ham[1:9 :3]
함수호출
O : spam(1)
X : spam (1)
변수할당
O :
x = 1
y = 2
long_variable = 3
X :
x = 1
y = 2
long_variable = 3다른 연산자 함께 사용시 우선순위가
낮은 연산자에 공백사용
O :
i = i + 1
submitted += 1x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)
X :
i=i+1
submitted +=1
x = x * 2 - 1
hypot2 = x * x + y * y
c = (a + b) * (a - b)
디폴트값 할당
O :
def complex(real, imag=0.0):
return magic(r=real, i=imag)X :
def complex(real, imag = 0.0):
return magic(r = real, i = imag)
'Python > Python Programming' 카테고리의 다른 글
[Python] strip, rstrip, lstrip - 공백과 문자 제거 함수 (0) 2018.05.19 [Python] Gmail 보내기 (0) 2018.05.18 [Python] 단축평가(short-circuit evalution) (0) 2018.05.15 [Python] ==(같다) 와 is (같다) 차이 (0) 2018.05.15 [Python] 문자열 100개씩 잘라서 출력하기 (0) 2018.05.14 [Python] 반복가능 (iterable)한 객체 오른쪽(right) 순회 (0) 2018.05.13 [Python] 리스트에서 숫자요소의 인덱스 위치 구하기 (0) 2018.05.13 [Python] 정규식 전방탐색과 후방탐색을 이용한 문자열 분할 (0) 2018.05.13