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

首頁 > 編程 > Python > 正文

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

2019-11-25 16:26:18
字體:
來源:轉載
供稿:網友

前言

最近工作中遇到個需求是要得到一個類的靜態屬性,也就是說有個類 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中得到類屬性列表的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 松江区| 龙里县| 桂东县| 贺州市| 台南县| 万州区| 哈尔滨市| 开封市| 宁夏| 通辽市| 陆良县| 昔阳县| 依兰县| 永春县| 区。| 和政县| 安多县| 夹江县| 长汀县| 鸡泽县| 通山县| 普兰店市| 通道| 团风县| 澳门| 丹凤县| 德江县| 杂多县| 建瓯市| 平武县| 大同县| 靖安县| 琼中| 吴旗县| 长寿区| 墨江| 株洲市| 佛山市| 油尖旺区| 定远县| 乌兰县|