- Secret Recipes of the Python Ninja
- Cody Jackson
- 283字
- 2021-06-25 22:14:43
How it works...
In this example, the whole video package comprises two sub-packages, video formats and video effects, with video effects having several sub-packages of its own. Within each package, each .py file is a separate module. During module importation, Python looks for packages on sys.path.
The inclusion of the __init__.py files is necessary so Python will treat the directories as packages. This prevents directories with common names from shadowing Python modules further along the search path. They also allow calling modules as stand-alone programs via the -m option, when calling Python programs.
Initialization files are normally empty but can contain initialization code for the package. They can also contain an __all__ list, which is a Python list of modules that should be imported whenever from <package> import * is used.
The reason for __all__ is for the developer to explicitly indicate which files should be imported. This is to prevent excessive delay from importing all modules within a package that aren't necessarily needed for other developers. It also limits the chance of undesired side-effects when a module is inadvertently imported. The catch is, the developer needs to update the __all__ list every time the package is updated.
Relative imports are based on the name of the current module. As the main module for a program always has the name "__main__", any modules that will be the main module of an application must use absolute imports.
To be honest, it is generally safer to use absolute imports just to make sure you know exactly what you're importing; with most development environments nowadays providing suggestions for paths, it is just as easy to write out the auto-populated path as it is to use relative paths.
- JavaScript百煉成仙
- Hands-On Image Processing with Python
- C/C++算法從菜鳥(niǎo)到達(dá)人
- Arduino開(kāi)發(fā)實(shí)戰(zhàn)指南:LabVIEW卷
- R語(yǔ)言游戲數(shù)據(jù)分析與挖掘
- Java持續(xù)交付
- Highcharts Cookbook
- MATLAB 2020從入門(mén)到精通
- Apache Camel Developer's Cookbook
- Advanced UFT 12 for Test Engineers Cookbook
- QlikView Unlocked
- Kotlin進(jìn)階實(shí)戰(zhàn)
- 人人都能開(kāi)發(fā)RPA機(jī)器人:UiPath從入門(mén)到實(shí)戰(zhàn)
- Neo4j 3.x入門(mén)經(jīng)典
- 深入大型數(shù)據(jù)集:并行與分布化Python代碼