查看python类的属性
来源:互联网 发布:法属圭亚那算法国嘛 编辑:程序博客网 时间:2024/06/09 16:11
查看一个类的静态属性,也就是说有个类 Type ,我要动态获取 Type.FTE 这个属性的值。
最简单的方案有两个:
getattr(Type, 'FTE')Type.__dict__['FTE']
那么,如果要获取类属性的列表,该怎么做呢?
首先上场的是 dir ,它能返回当前范围的所有属性名称列表:
>>> dir()['__builtins__', '__doc__', '__name__', '__package__']>>> dir(list)['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
可以配合使用 inspect 包中的功能来过滤:
>>> [i for i in dir(list) if inspect.isbuiltin(getattr(list, i))]['__new__', '__subclasshook__']
inspect 包中还包含:
>>> [i for i in dir(inspect) if inspect.isfunction(getattr(inspect, i))]['_searchbases', 'classify_class_attrs', 'cleandoc', 'findsource', 'formatargspec', 'formatargvalues', 'getabsfile', 'getargs', 'getargspec', 'getargvalues', 'getblock', 'getcallargs', 'getclasstree', 'getcomments', 'getdoc', 'getfile', 'getframeinfo', 'getinnerframes', 'getlineno', 'getmembers', 'getmodule', 'getmoduleinfo', 'getmodulename', 'getmro', 'getouterframes', 'getsource', 'getsourcefile', 'getsourcelines', 'indentsize', 'isabstract', 'isbuiltin', 'isclass', 'iscode', 'isdatadescriptor', 'isframe', 'isfunction', 'isgenerator', 'isgeneratorfunction', 'isgetsetdescriptor', 'ismemberdescriptor', 'ismethod', 'ismethoddescriptor', 'ismodule', 'isroutine', 'istraceback', 'joinseq', 'namedtuple', 'stack', 'strseq', 'trace', 'walktree']
还可以配合 callable 来使用:
>>> [i for i in dir(inspect) if not callable(getattr(inspect, i))]['CO_GENERATOR', 'CO_NESTED', 'CO_NEWLOCALS', 'CO_NOFREE', 'CO_OPTIMIZED', 'CO_VARARGS', 'CO_VARKEYWORDS', 'TPFLAGS_IS_ABSTRACT', '__author__', '__builtins__', '__date__', '__doc__', '__file__', '__name__', '__package__', '_filesbymodname', 'dis', 'imp', 'linecache', 'modulesbyfile', 'os', 're', 'string', 'sys', 'tokenize', 'types']
上面提到了 dict ,也可以用它来获取属性列表:
>>> list.__dict__.keys()['__getslice__', '__getattribute__', 'pop', 'remove', '__rmul__', '__lt__', '__sizeof__', '__init__', 'count', 'index', '__delslice__', '__new__', '__contains__', 'append', '__doc__', '__len__', '__mul__', 'sort', '__ne__', '__getitem__', 'insert', '__setitem__', '__add__', '__gt__', '__eq__', 'reverse', 'extend', '__delitem__', '__reversed__', '__imul__', '__setslice__', '__iter__', '__iadd__', '__le__', '__repr__', '__hash__', '__ge__']
阅读全文
0 0
- 查看python类的属性
- Python--类的属性
- Python类的属性
- python-类的属性
- python类的属性
- python查看类的定义
- python的类属性和实例属性
- python的类属性和实例属性
- python 的 类属性 与 实例属性
- python的类属性和实例属性
- python的类属性和实例属性
- python的类属性和实例属性
- Python Class的实例属性/类属性
- 8 python 定制自己的类,实现查看属性,按index取值,迭代,切片等个性化类
- python中类的属性
- python 类的数据属性
- Python 类属性的理解
- python的类属性问题
- 读《head first Python》体会
- C++ 实现计算机程序的实现及其细节处理
- centos7修改字符集
- 使用iframe和layer实现弹框
- Java对象
- 查看python类的属性
- Shiro——认证策略
- 卡方检验原理及应用
- Spring Cache与Redis结合使用
- mysql 之 sql 语句 之 数据的增删改查
- Java异常处理
- 计算机基础---时钟
- 常用布局效果02-云资源购买信息展示布局
- Self-Numbers解题报告