国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python中如何獲取類屬性的列表

2020-02-23 04:11:48
字體:
供稿:網(wǎng)友

前言

最近工作中遇到個需求是要得到一個類的靜態(tài)屬性,也就是說有個類 Type ,我要動態(tài)獲取 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__']            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 锡林郭勒盟| 东台市| 藁城市| 洪洞县| 兴义市| 宣武区| 兖州市| 远安县| 民丰县| 宜丰县| 铜川市| 寿阳县| 福贡县| 榆中县| 台江县| 车致| 龙岩市| 三明市| 柯坪县| 惠安县| 庄浪县| 竹溪县| 米林县| 安图县| 抚远县| 广东省| 临清市| 香格里拉县| 远安县| 濮阳市| 简阳市| 罗甸县| 确山县| 广水市| 海安县| 永平县| 张家川| 平和县| 彝良县| 左权县| 新平|