Python import

 from package import item VS import package.item

There is nothing wrong with using from Package import specific submodule! In fact this becomes the recommended notation unless the importing module needs to use submodules with the same name from different packages.


1. Note that when using from package import item, the item can be either a submodule (or subpackage) of the package, or some other name defined in a the package, like a function, class or variable.

2. Contrarily, when using syntax like import item.subitem.subsubitem, each item except for thelast must be a package; the last item can be a module or a package butcan't be a class or function or variable defined in the previous item.


Package related:

1. The __init__.py files are required to make Python treat the directories as containing packages;

this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path.

2.  from package import *

If __all__ is defined, "from package import * " will import it's named submodules of the package.

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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值