发布于 2015-08-30 07:53:35 | 153 次阅读 | 评论: 1 | 来源: 网络整理

问题

You want precise control over the symbols that are exported from a module or package when a user uses the from module import * statement.


解决方案

Define a variable __all__ in your module that explicitly lists the exported names. For example:

# somemodule.py
def spam():
    pass

def grok():
    pass

blah = 42
# Only export 'spam' and 'grok'
__all__ = ['spam', 'grok']

讨论

Although the use of from module import * is strongly discouraged, it still sees frequent use in modules that define a large number of names. If you don’t do anything, this form of import will export all names that don’t start with an underscore. On the other hand, if __all__ is defined, then only the names explicitly listed will be exported.

If you define __all__ as an empty list, then nothing will be exported. An AttributeEr ror is raised on import if __all__ contains undefined names.

最新网友评论  共有(1)条评论 发布评论 返回顶部
PHPERZ网友 发布于2016-06-28 01:30:49
能翻译成中文吗
支持(0)  反对(0)  回复

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务