4.8. Intermezzo : Coding Style
간주곡 : 코딩 스타일
Now that you are about to write longer, more complex pieces of Python, it is a good time to talk about coding style. Most languages can be written (or more concise, formatted) in different styles; some are more readable than others. Making it easy for others to read your code is always a good idea, and adopting a nice coding style helps tremendously for that.
이제 우리는 파이썬의 좀더 길고 복잡한 코드를 쓰는 법에 대해 알게 되었습니다. 코딩 스타일에 대해 이야기 하기 좋은 시점이지요.
많은 프로그래밍 언어들에는 다양한 스타일이 있습니다. 어떤 스타일은 다른 것보다 더 읽기 쉽습니다. 당신의 코드를 다른 사람들도 쉽게 읽을 수 있는 것이 좋습니다. 좋은 코딩 스타일을 채택하면 코드를 읽는 데 큰 도움이 됩니다.
For Python, PEP 8 has emerged as the style guide that most projects adhere to; it promotes a very readable and eye-pleasing coding style. Every Python developer should read it at some point; here are the most important points extracted for you:
PEP 8은 파이썬을 위한 스타일 가이드로 많은 프로젝트에서 지지받고 있습니다. PEP 8은 무척 읽기 편하고 눈에도 즐거운 코딩 스타일을 제안합니다. 파이썬 개발자라면 그 글을 꼭 읽어봐야 합니다. 가장 중요한 부분들만 추출해 둔 다음 내용에 초점을 맞추어 PEP 8을 읽어주십시오.
Use 4-space indentation, and no tabs.
- 4번의 space 키를 들여쓰기로 사용하고, tabs은 사용하지 않습니다.
4 spaces are a good compromise between small indentation (allows greater nesting depth) and large indentation (easier to read). Tabs introduce confusion, and are best left out.
- 4번의 space는 작은 들여쓰기와 큰 들여쓰기 사이의 적절한 타협점입니다. (작은 들여쓰기는 깊이있는 중첩에, 큰 들여쓰기는 가독성에 유용합니다.) tab은 혼란을 유발합니다. (설정값이 컴퓨터 마다 다름)
Wrap lines so that they don’t exceed 79 characters. This helps users with small displays and makes it possible to have several code files side-by-side on larger displays.
- 한 줄에 79자를 초과하지 않는 것을 추천합니다. 그렇게 하면 작은 화면의 사용자에게 도움이 됩니다. 또한 큰 화면 사용자는 여러개의 코드 파일을 한 번에 볼 수 있습니다.
Use blank lines to separate functions and classes, and larger blocks of code inside functions.
- 함수와 클래스, 함수안의 큰 코드 블럭을 구분하기 위해 빈 줄을 사용해 주십시오.
When possible, put comments on a line of their own.
- 가능하다면, 코멘트는 그 코멘트가 가리키는 줄에 남기는 것이 좋습니다.
Use docstrings.
- docstrings을 작성해 주십시오.
Use spaces around operators and after commas, but not directly inside bracketing constructs:
a = f(1, 2) + g(3, 4)
.- 연산기호 주변과 콤마 뒤에는 공백을 둡니다. 하지만 괄호로 묶이는 구조의 시작과 끝에는 사용하지 않습니다.
Name your classes and functions consistently; the convention is to use
CamelCase
for classes andlower_case_with_underscores
for functions and methods. Always useself
as the name for the first method argument (see A First Look at Classes for more on classes and methods).- 클래스와 함수의 이름을 일관성 있게 짓습니다. 언제나 첫번째 메소드 인수의 이름으로는
self
를 사용합니다. (클래스와 메소드에 대한 더 많은 내용은 A First Look at Classe를 참고하십시오. 아직 연결되지 않음)
- 클래스와 함수의 이름을 일관성 있게 짓습니다. 언제나 첫번째 메소드 인수의 이름으로는
Don’t use fancy encodings if your code is meant to be used in international environments. Python’s default, UTF-8, or even plain ASCII work best in any case.
- 국제 환경에서 코드를 사용하기 원한다면 인코딩에 주의하십시오. 파이썬의 기본설정인 UTF-8과 ASCII를 사용하는 것이 가장 안전합니다.
Likewise, don’t use non-ASCII characters in identifiers if there is only the slightest chance people speaking a different language will read or maintain the code.
- 마찬가지로, 다른 언어를 접할 기회가 적은 사람들이 코드를 읽고 유지해야 한다면, 식별자로 ASCII 가 아닌 문자를 사용하지 마십시오.