Python Modules

[Python Modules]

 1. a module is a python source file. 

 2. a package is a directory with a __init__.py file within it.

  1) 如果目录下没有__init__.py文件, 则python不会认为这个目录是个package, 这意味着 import package 以及 from package import * 以及 from package import xxx 都会失败.

  2) 无论是import package还是from package import *都会执行__init__.py, 不同的是, 当执行from package import *时, python会自动import __all__中的文件.

  3)__all__对于单一的Module同样有效

 3. import哪个符号, 那么该符号就会加入当前作用域中.
 4. from module import *,不会导入以 "_"(单下划线开头的名字),“__”(双下划线)的name mangling只在Class的命名空间中才有

  

  虽然import *不会导入"_"前缀的名字,但是import module的话仍旧可以访问到,所以在一个module中,不能以“_”的手段来定义私有变量

参考:

1、http://www.blogjava.net/cpegtop/articles/383806.html

Importing * From a Package

  If __all__ is not defined, the statement from sound.effects import * does not import all submodules from the package sound.effects into the current namespace; it only ensures that the package sound.effects has been imported (possibly running any initialization code in __init__.py) and then imports whatever names are defined in the package. This includes any names defined (and submodules explicitly loaded) by __init__.py.

  from xxx import * 不会导入xxx包下的所有submodule,除非该submodule在__init__.py中会被引用到。

Intra-package References

  

原文地址:https://www.cnblogs.com/tekkaman/p/3316751.html