연관성이 있는 코드끼리 분리 하는데(코드 내 연관성이 있는 코드 덩어리 묶는데) 사용한다.
º 최상위 위치에 있는 함수와 클래스의 정의로 두 개의 공백 라인(줄바꿈)을 사용하는 방식
class MyFirstClass:
pass
class MySecondClass:
pass
º 클래스 내부의 메서드 정의로 한 개의 공백 라인(줄바꿈)을 사용하여 구분하는 방식
class MyClass:
def first_method(self):
return None
def second_method(self):
return None
소스 파일 인코딩(Source File Encoding)
Python 배포 코드는 UTF-8을 사용해야 한다. (별도의 인코딩을 사용 시 명시 할 것)
Python 2 에서는 ASCII를 사용한다.
표준 라이브러리에서 UTF-8이 아닌 문자 인코딩은 테스트목적으로만 사용해야한다.
ASCII가 아닌 문자는 가급적 장소, 사람, 이름을 나타낼 때만 사용해야한다.
ASCII가 아닌 문자를 데이터로 사용하는 경우, ( z̯̯͡a̧͎̺l̡͓̫g̹̲o̡̼̘ ) 와 같이 보기 힘든 유니코드 문자로 사용하지 말아야한다.
Python 표준 라이브러리의 모든 식별자는 ASCII 전용 식별자를 사용해야 한다.
가능한 영어 단어로 사용해야한다. (그럼에도 불구하고 대부분 영어단어가 아닌 약어와 기술 용어가 사용됨)
전 세계 사용자를 대상으로 하는 오픈 소스 프로젝트는 위와 같은 내용을 채택하는 것이 좋다.
라이브러리 가져오기(Imports)
import는 가장 맨 위에 선언해야한다.
한줄에 한개의 Import를 권장한다.
from으로 동일한 곳에서 여러 모듈을 import할 때 한 줄로 사용할 수 있다.
import는 다음과 같은 순서로 그룹화 해야한다.
작성중 ...
Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.Imports should be grouped in the following order:
Standard library imports.
Related third party imports.
Local application/library specific imports.
You should put a blank line between each group of imports.
Absolute imports are recommended, as they are usually more readable and tend to be better behaved (or at least give better error messages) if the import system is incorrectly configured (such as when a directory inside a package ends up onsys.path):
However, explicit relative imports are an acceptable alternative to absolute imports, especially when dealing with complex package layouts where using absolute imports would be unnecessarily verbose:
import mypkg.sibling
from mypkg import sibling
from mypkg.sibling import example
from . import sibling
from .sibling import example
from myclass import MyClass
from foo.bar.yourclass import YourClass
import myclass
import foo.bar.yourclass
º 한줄에 한개의 Import를 권장한다.
import os
import sys
º from으로 동일한 곳에서 여러 모듈을 Import할 때 한 줄로 사용할 수 있다.
from subprocess import Popen, PIPE
모듈 (Module Level Dunder Names)
__모듈이름__ 형대로 사용한다.
ㅁ
문자열 따옴표(String Quotes)
들여 쓰기(Indentation)
들여 쓰기는 4개의 공백(스페이스)을 사용한다. (선택 사항)
Tap 사용은 이미 작성된 코드에서 들여 쓰기를 Tap으로 사용했을 경우만 사용한다. (일관성 유지)
Python3에서는 Tap과 Space를 혼용해서 쓰지 않도록 한다.
표현식 및 구문의 공백(Whitespace in Expressions and Statements)
*구문(Statements) : 컴퓨터에서, 원시 언어의 문장을 올바르게 구성하기 위한 규칙. 자연어의 문법에 해당한다.