便捷提取python导入包的属性方法
2018-12-08 08:02
555 查看
很多时候我们都需要了解下python中导入包的属性方法信息,当然dir 是最便捷的了,不过如果想知道特定的,例如以_ 开头的属性,需要写个筛选,以下是实现筛选的两种方式,主要是练习下yield from 的使用,可以聊作参考~
#法1 def e(start='_', module='os'): module = __import__(module) def gen_attr(): for attr in dir(module): if attr.startswith(start): yield attr yield from gen_attr() #法2 利用生成器推导式更简洁 def e2(start='', module='os'): module = __import__(module) yield from (attr for attr in dir(module) if attr.startswith(start)) if __name__ == '__main__': print (list(e(''))) print (list(e2('a')))
以上这篇便捷提取python导入包的属性方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- 便捷提取python导入包的属性
- python直接访问私有属性的简单方法
- python中7种实现单例模式的方法:staticmethod、classmethod、类属性方法、__new__、装饰器、元类、名字覆盖
- 【Python】从字符串中提取字母字符串的几种方法
- python3 类的属性方法
- 全面了解python中的类,对象,方法,属性
- Python属性和方法
- python动态添加属性和方法
- 【Python】 属性的 get 与 set 方法
- python request属性及方法说明
- python多进程提取处理大量文本的关键词方法
- Python面向对象之静态属性、类方法与静态方法分析
- Python模块的导入方法1
- Python自定义模块导入方法
- python进阶学习笔记(四)——python中访问限制、创建类属性、定义实例方法、定义类方法、类的继承
- Python_两种导入模块的方法异同
- Python---模块本质,导入方法,优化,与包的导入
- python 函数、模块、包及import导入方法
- Python中的模块导入和读取键盘输入的方法
- [Dynamic Language] Python 静态方法、类方法、属性