Python中如何获取类属性的列表

yipeiwu_com5年前Python基础

前言

最近工作中遇到个需求是要得到一个类的静态属性,也就是说有个类 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__']

总结

以上就是在Python中得到类属性列表的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

相关文章

python对url格式解析的方法

本文实例讲述了python对url格式解析的方法。分享给大家供大家参考。具体分析如下: python针对url格式的解析,可根据指定的完整URL解析出url地址的各个部分 from...

利用Celery实现Django博客PV统计功能详解

前言 前几天给网站的文章增加了pv统计,之前只有uv统计。之前没加pv统计是觉得每个用户每访问一次文章,我都需要做一次数据库写操作实在是有损性能,毕竟从用户在the5fire博客的的一...

pytorch使用Variable实现线性回归

pytorch使用Variable实现线性回归

本文实例为大家分享了pytorch使用Variable实现线性回归的具体代码,供大家参考,具体内容如下 一、手动计算梯度实现线性回归 #导入相关包 import torch as...

python-视频分帧&多帧合成视频实例

我就废话不多说了,直接上代码吧! 1.视频分帧: import cv2 vidcap = cv2.VideoCapture('005.avi') success,image = v...

python实现12306抢票及自动邮件发送提醒付款功能

python实现12306抢票及自动邮件发送提醒付款功能

#写在前面,这个程序我已经弄出来了,但是因为黄牛泛滥以及懒人太多,整个程序的代码就不贴出来了,这里纯粹就是技术交流。 只做技术交流、、、、、 嗯,程序结束后,自己还是得手动付款。 废...