파이썬`import x`와`from x import y` 문을 정렬하는 올바른 방법은 무엇입니까?
파이썬 스타일 가이드는 이 같은 그룹 수입에 제안한다 :
수입품은 다음 순서로 그룹화해야합니다.
- 표준 라이브러리 가져 오기
- 관련 제 3 자 수입
- 로컬 애플리케이션 / 라이브러리 특정 가져 오기
그러나 두 가지 수입 방법을 어떻게 배치해야하는지 언급하지 않았다.
from foo import bar
import foo
여러 가지 방법으로 정렬 할 수 있습니다 (모든 가져 오기가 동일한 그룹에 속하는 것으로 가정).
첫째
from..import
, 다음import
from g import gg from x import xx import abc import def import x
첫째
import
, 다음from..import
import abc import def import x from g import gg from x import xx
가져 오기의 종류를 무시하고 모듈 이름별로 알파벳 순서
import abc import def from g import gg import x from xx import xx
PEP8은 이것에 대한 선호 순서를 언급하지 않고 "정리 가져 오기"기능 일부 IDE는 아마도 그 기능의 개발자가 선호하는 것을 수행했을 것입니다.
BDFL (또는 다른 Python 핵심 개발자)의 의견이나 전자 메일을 설명하는 다른 PEP를 찾고 있습니다. 자신의 선호도를 나타내는 주관적인 답변을 게시하지 마십시오.
수입품은 일반적으로 알파벳순으로 정렬되며 PEP 8 이외의 다양한 장소에 설명되어 있습니다.
알파벳순으로 정렬 된 모듈은 더 빨리 읽고 검색 할 수 있습니다. 결국 파이썬은 가독성에 관한 것입니다. 또한 무언가를 가져 왔는지 확인하는 것이 더 쉽고 중복 된 가져 오기를 피합니다
PEP 8에는 정렬과 관련하여 사용할 수있는 것이 없으므로 사용하는 선택에 관한 모든 것이 있습니다.
평판이 좋은 사이트와 리포지토리에서도 인기가 거의 없지만 알파벳 순서가 있습니다.
예를 들면 다음과 같습니다.
import httplib
import logging
import random
import StringIO
import time
import unittest
from nova.api import openstack
from nova.auth import users
from nova.endpoint import cloud
또는
import a_standard
import b_standard
import a_third_party
import b_third_party
from a_soc import f
from a_soc import g
from b_soc import d
Reddit official repository also states that, In general PEP-8 import ordering should be used. However there are a few additions which is
for each imported group the order of imports should be:
import <package>.<module> style lines in alphabetical order
from <package>.<module> import <symbol> style in alphabetical order
References:
- https://code.google.com/p/soc/wiki/PythonStyleGuide
- https://github.com/reddit/reddit/wiki/PythonImportGuidelines
- http://docs.openstack.org/developer/hacking/
- http://developer.plone.org/reference_manuals/external/plone.api/contribute/conventions.html#grouping-and-sorting
PS: the isort utility automatically sorts your imports.
According to the CIA's internal coding conventions (part of the WikiLeaks Vault 7 leak), python imports should be grouped into three groups:
- Standard library imports
- Third-party imports
- Application-specific imports
Imports should be ordered lexicographically within these groups, ignoring case:
import foo
from foo import bar
from foo.bar import baz
from foo.bar import Quux
from Foob import ar
The PEP 8 says nothing about it indeed. There's no convention for this point, and it doesn't mean the Python community need to define one absolutely. A choice can be better for a project but the worst for another... It's a question of preferences for this, since each solutions has pro and cons. But if you want to follow conventions, you have to respect the principal order you quoted:
- standard library imports
- related third party imports
- local application/library specific imports
For example, Google recommend in this page that import should be sorted lexicographically, in each categories (standard/third parties/yours). But at Facebook, Yahoo and whatever, it's maybe another convention...
All import x
statements should be sorted by the value of x
and all from x import y
statements should be sorted by the value of x
in alphabetical order and the sorted groups of from x import y
statements must follow the sorted group of import x
statements.
import abc
import def
import x
from g import gg
from x import xx
from z import a
'IT' 카테고리의 다른 글
투명한 ARGB 16 진수 값 (0) | 2020.06.21 |
---|---|
프로덕션에서 Docker 컨테이너를 확장하는 방법 (0) | 2020.06.21 |
HTML5 응용 프로그램을 데스크톱 응용 프로그램으로 배포하기위한 솔루션? (0) | 2020.06.21 |
나만의 반복자 만들기 (0) | 2020.06.21 |
드롭 파일을 표준 html 파일 입력으로 드래그 (0) | 2020.06.21 |