国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 西乌| 彭泽县| 龙井市| 秦皇岛市| 乌海市| 玉林市| 淮南市| 临沭县| 普宁市| 玉环县| 清流县| 宁明县| 视频| 德惠市| 台东县| 五台县| 剑阁县| 宣化县| 闽清县| 牙克石市| 邵武市| 平远县| 淄博市| 平昌县| 镇原县| 延寿县| 汉寿县| 海安县| 浮梁县| 南郑县| 博野县| 巫山县| 府谷县| 达拉特旗| 商都县| 义马市| 读书| 眉山市| 那坡县| 杂多县| 湖北省|