点击此处浏览总目录

模块导入限制

__all__

  __all__可用于模块导入时限制,如:

  
  from module import *

  此时被导入模块module中

    若定义了__all__属性,则只有all内指定的属性、方法、类可被导入
    若没定义__all__属性,则模块内的所有将被导入

 

使用示例

  a.py

    __all__ = ['test1']

    def test1():
        print("-----test1-----")


    def test2():
        print("-----test2-----")

  b.py

    from a import *

    test1()  #输出:-----test1-----
    test2()  #报错,name 'test2' is not defined

 

注意: __all__ 只影响到了 from <module> import * 这种导入方式
    对于如下导入方式并没有影响,仍然可以从外部导入
    1) from <module> import <member>
    2) import <module>

 

--------------------------------------------------------------------------------------------------------------------

posted @ 2019-07-04 15:56  立业的博客  阅读(206)  评论(0编辑  收藏  举报