파이썬에서 들여쓰기는 탭 & 스페이스 중 어느걸로 하시나요?
조회수 8614회
저는 파이썬 코드 짤때 탭으로 들여쓰기 했는데
누가 탭으로 하면 에디터들간에 탭 길이가 다르니까 스페이스로 띄우는게 좋다고 해서 물어봅니다
다른 파이썬 개발자들은 어떻게 쓰고 있나요?
진짜로 에디터들간에 탭 길이가 달라서 충돌이 있을 수 있나요?
1 답변
-
PEP-8 Tabs or Spaces?에서는 스페이스를 쓰라고 합니다
Spaces are the preferred indentation method. Tabs should be used solely to remain consistent with code that is already indented with tabs. Python 3 disallows mixing the use of tabs and spaces for indentation. Python 2 code indented with a mixture of tabs and spaces should be converted to using spaces exclusively. When invoking the Python 2 command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!
주로 space를 쓰지만 이미 tab을 쓴 코드에서는 일관성 유지를 위해 계속 tab을 쓰는게 낫다고 합니다.
python3에선 space와 tab을 혼용해도 되지만 python2 쉘에서는 IndentationError가 나는군요.
python2 쉘에서 혼용한 결과
>>> def foo(): ... print "나는 탭!" ... print "나는 스페이스!" File "<stdin>", line 3 print "나는 스페이스!" ^ IndentationError: unindent does not match any outer indentation level >>>
댓글 입력