模块导入限制

__all__spa

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

  
  from module import *blog

  此时被导入模块module中class

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

 

使用示例import

  a.pymodule

    __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>im

 

--------------------------------------------------------------------------------------------------------------------ember

相关文章
相关标签/搜索