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__的基础上增加的新的方法。
  • 相关阅读:
    App Submission Issues
    番茄钟App(Pomodoro Tracker)
    WatchKit App Submission Issues
    Watch​Kit Learning Resources
    Scrum Planning Card
    How to get cocoapods work on Yosemite
    如何在DigitalOcean安装Ghost
    Swift学习资源
    Xcode 6 模拟器路径
    【Android-4】Scheme跳转协议
  • 原文地址:https://www.cnblogs.com/fanxiaojuan/p/5210567.html
Copyright © 2011-2022 走看看