advanced points from open source python projects

mainly for open source mongoengine

(1)fallback import

(2)import from the future

(3)customize imports by _ _ all _ _
the situation: sometimes, it does not make much sense that exporting utility functions and classes to external code. in order to control what objects get exposed when you import a module like this, you can specify _ _ all _ _ somewhere in this module.

All that you need to do is supply a listor some other sequence—that contains the names of objects that should get imported when the module is imported using an asterisk(*)

#module a
__all__ = ['func1']
def func1():
    return

def func2():
    return 0

#module b
from a import *
func1() #OK
func2() #NOK

(4)absolute/relative(intra-package) import
  about imports in Python PEP328(multiple-lines,absolute/relative) and PEP338(executing modules as scripts) and PEP366(main modules explicit relative imports)
   (a)absolute import
For example, the statement import pkg_or_module_name may be ambiguous.
To resolve ambiguity, it is proposed that pkg_or_module_name will always be a module or package reachable from sys.path, which is called an absolute import.
   (b)relative import
Note: it is some difficulty to directly execute a module with relative import as a top-level script

(5)script&module
a python files may be loaded in two styles, either as a top-level script or as a module.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值