zoukankan      html  css  js  c++  java
  • 模块导入限制

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

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

  • 相关阅读:
    [ Docker ] 基础安装使用及架构
    [ Docker ] 基础概念
    Nginx
    ELK
    SVN + Jenkins 构建自动部署
    Zabbix
    ELK
    ELK 部署文档
    vue.js在visual studio 2017下的安装
    vue.js是什么
  • 原文地址:https://www.cnblogs.com/shiliye/p/11132937.html
Copyright © 2011-2022 走看看