zoukankan      html  css  js  c++  java
  • __all__有趣的属性

    python有趣的属性
    __all__可用于模块导入时限制,如:
    from module import *
    此时被导入模块若定义了__all__属性,则只有all内指定的属性、方法、类可被导入;
    若没定义,则模块内的所有将被导入。

    例子:比如python中的unittest模块

    __all__ = ['TestResult', 'TestCase', 'TestSuite',
    'TextTestRunner', 'TestLoader', 'FunctionTestCase', 'main',
    'defaultTestLoader', 'SkipTest', 'skip', 'skipIf', 'skipUnless',
    'expectedFailure', 'TextTestResult', 'installHandler',
    'registerResult', 'removeResult', 'removeHandler']

    # Expose obsolete functions for backwards compatibility
    __all__.extend(['getTestCaseNames', 'makeSuite', 'findTestCases'])

    print unittest.__all__

    结果:['TestResult', 'TestCase', 'TestSuite', 'TextTestRunner', 'TestLoader', 'FunctionTestCase', 'main', 'defaultTestLoader', 'SkipTest', 'skip', 'skipIf', 'skipUnless', 'expectedFailure', 'TextTestResult', 'installHandler', 'registerResult', 'removeResult', 'removeHandler',
       'getTestCaseNames', 'makeSuite', 'findTestCases']
    如果某个.py import了unittest后,这个模块中的类、成员、方法、属性将被引入。
    其中__all__.extend为在原来__all__的基础上增加的新的方法。
  • 相关阅读:
    实现一个简单的Form授权 How to: Implement Simple Forms Authentication
    寄存器寻址方式
    HDU2094 产生冠军
    HDU1060 Leftmost Digit 数论
    HDU1496 Equations [hash]
    HDU1298 T9 字典树 DFS
    HDU1051 Wooden Sticks
    HDU1800 Flying to the Mars
    HDU1285 确定比赛名次 拓扑排序
    HDU1716 排列2 组合数
  • 原文地址:https://www.cnblogs.com/fanxiaojuan/p/5210567.html
Copyright © 2011-2022 走看看