__all__有趣的属性
2024-09-02 18:18:53
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__的基础上增加的新的方法。
最新文章
- Xamarin Studio在Mac环境下的配置和Xamarin.iOS常用控件的示例
- Base64编码格式详解
- Travis-CI与Latex构建开源中文PDF
- UIView背景渐变三种方法
- 修改 apache http server 默认站点目录
- wireshark的ubuntu更新ppa源
- An error has occurred,See error log for more details
- SQL点滴26—常见T-SQL面试解析
- [转]Creating an iPhone Daemon
- java byte【】数组与文件读写
- GoodReads: Machine Learning (Part 3)
- 基于docker+reveal.js搭建一个属于自己的在线ppt网站
- google软件测试之道读后感(二)
- 【BZOJ4805】欧拉函数求和(杜教筛)
- 通过游戏来学习CSS的Flex布局
- Unity3d自制字体
- GUI之ScrollView的使用
- Latex: IEEEtrans模板下 扩大标题宽度
- PHP开发安全问题总结
- day3之文件操作