올바른 / 올바른 패키지 __init__.py 파일을 작성하는 방법
내 패키지의 구조는 다음과 같습니다.
mobilescouter/
__init__.py #1
mapper/
__init__.py #2
lxml/
__init__.py #3
vehiclemapper.py
vehiclefeaturemapper.py
vehiclefeaturesetmapper.py
...
basemapper.py
vehicle/
__init__.py #4
vehicle.py
vehiclefeature.py
vehiclefeaturemapper.py
...
__init__.py
파일을 올바르게 작성 하는 방법을 잘 모르겠습니다 . 외모가 좋아 :__init__.py #1
__all__ = ['mapper', 'vehicle']
import mapper
import vehicle
그러나 예를 들어 어떻게 보 __init__.py #2
일까요? 광산은 :
__all__ = ['basemapper', 'lxml']
from basemaper import *
import lxml
언제 사용해야 __all__
합니까?
__all__
매우 좋습니다-모듈을 자동으로 가져 오지 않고 가져 오기 명령문을 안내하는 데 도움이됩니다. http://docs.python.org/tutorial/modules.html#importing-from-a-package
사용 __all__
및 import *
중복, __all__
필요한 것만
내가 사용하는 가장 강력한 이유 중 하나라고 생각 import *
에서 __init__.py
수입 패키지에 대한 기존의 응용 프로그램을 깨지 않고 여러 스크립트로 성장했습니다 스크립트를 리팩토링 할 수있을 것입니다. 그러나 처음부터 패키지를 디자인하는 경우. __init__.py
파일을 비워 두는 것이 가장 좋습니다 .
예를 들면 다음과 같습니다.
foo.py - contains classes related to foo such as fooFactory, tallFoo, shortFoo
그런 다음 앱이 커지고 이제는 전체 폴더입니다.
foo/
__init__.py
foofactories.py
tallFoos.py
shortfoos.py
mediumfoos.py
santaslittlehelperfoo.py
superawsomefoo.py
anotherfoo.py
초기화 스크립트는
__all__ = ['foofactories', 'tallFoos', 'shortfoos', 'medumfoos',
'santaslittlehelperfoo', 'superawsomefoo', 'anotherfoo']
# deprecated to keep older scripts who import this from breaking
from foo.foofactories import fooFactory
from foo.tallfoos import tallFoo
from foo.shortfoos import shortFoo
변경 중에 다음을 수행하도록 작성된 스크립트가 중단되지 않도록합니다.
from foo import fooFactory, tallFoo, shortFoo
My own __init__.py
files are empty more often than not. In particular, I never have a from blah import *
as part of __init__.py
-- if "importing the package" means getting all sort of classes, functions etc defined directly as part of the package, then I would lexically copy the contents of blah.py
into the package's __init__.py
instead and remove blah.py
(the multiplication of source files does no good here).
If you do insist on supporting the import *
idioms (eek), then using __all__
(with as miniscule a list of names as you can bring yourself to have in it) may help for damage control. In general, namespaces and explicit imports are good things, and I strong suggest reconsidering any approach based on systematically bypassing either or both concepts!-)
Your __init__.py
should have a docstring.
Although all the functionality is implemented in modules and subpackages, your package docstring is the place to document where to start. For example, consider the python email
package. The package documentation is an introduction describing the purpose, background, and how the various components within the package work together. If you automatically generate documentation from docstrings using sphinx or another package, the package docstring is exactly the right place to describe such an introduction.
For any other content, see the excellent answers by firecrow and Alex Martelli.
참고URL : https://stackoverflow.com/questions/1944569/how-do-i-write-good-correct-package-init-py-files
'Programing' 카테고리의 다른 글
텍스트 뷰에 맞게 텍스트 글꼴 크기를 조정하는 방법 (0) | 2020.05.21 |
---|---|
자바 생성자 상속 (0) | 2020.05.21 |
EJB-언제 원격 및 / 또는 로컬 인터페이스를 사용해야합니까? (0) | 2020.05.21 |
부동 소수점 숫자가 왜 정확하지 않습니까? (0) | 2020.05.21 |
최대 절전 모드에서 분리 된 객체를 다시 부착하는 올바른 방법은 무엇입니까? (0) | 2020.05.21 |