Python中如何獲取類屬性的列表
前言
最近工作中遇到個(gè)需求是要得到一個(gè)類的靜態(tài)屬性,也就是說有個(gè)類 Type ,我要?jiǎng)討B(tài)獲取 Type.FTE
這個(gè)屬性的值。
最簡單的方案有兩個(gè):
getattr(Type, 'FTE') Type.__dict__['FTE']
那么,如果要獲取類屬性的列表,該怎么做呢?
首先上場的是 dir ,它能返回當(dāng)前范圍的所有屬性名稱列表:
>>> 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__']
總結(jié)
以上就是在Python中得到類屬性列表的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
- Python 列表(List)操作方法詳解
- Python統(tǒng)計(jì)列表中的重復(fù)項(xiàng)出現(xiàn)的次數(shù)的方法
- Python實(shí)現(xiàn)合并兩個(gè)列表的方法分析
- python提取字典key列表的方法
- Python列表list數(shù)組array用法實(shí)例解析
- python比較兩個(gè)列表是否相等的方法
- 在Python的列表中利用remove()方法刪除元素的教程
- Python中列表元素轉(zhuǎn)為數(shù)字的方法分析
- python 把列表轉(zhuǎn)化為字符串的方法
- Python字符串、元組、列表、字典互相轉(zhuǎn)換的方法
- Python去除列表中重復(fù)元素的方法
- Python中將字典轉(zhuǎn)換為列表的方法
- python分割列表(list)的方法示例
- 在Python中使用lambda高效操作列表的教程
- python獲取指定目錄下所有文件名列表的方法
- Python中使用copy模塊實(shí)現(xiàn)列表(list)拷貝
- python創(chuàng)建列表和向列表添加元素的實(shí)現(xiàn)方法
- Python列表操作方法詳解
相關(guān)文章
PyQt5學(xué)習(xí)之QThread類的使用詳解
QThread是Qt線程類中最核心的底層類。要使用QThrea開始一個(gè)線程,可以創(chuàng)建它的一個(gè)子類,然后覆蓋其QThread.run()函數(shù)。這篇文章就來和大家聊聊QThread類的使用,感興趣的可以學(xué)習(xí)一下2022-12-12Python中flatten( ),matrix.A用法說明
這篇文章主要介紹了Python中flatten( ),matrix.A用法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07python使用matplotlib:subplot繪制多個(gè)子圖的示例
這篇文章主要介紹了python使用matplotlib:subplot繪制多個(gè)子圖的示例,幫助大家更好的利用python繪制圖像,感興趣的朋友可以了解下2020-09-09python sys,os,time模塊的使用(包括時(shí)間格式的各種轉(zhuǎn)換)
這篇文章主要介紹了python sys,os,time模塊的使用(包括時(shí)間格式的各種轉(zhuǎn)換),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04numpy實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)反向傳播算法的步驟
這篇文章主要介紹了numpy實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)反向傳播算法的步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Python實(shí)現(xiàn)輸出程序執(zhí)行進(jìn)度百分比的方法
這篇文章主要介紹了Python實(shí)現(xiàn)輸出程序執(zhí)行進(jìn)度百分比的方法,涉及Python數(shù)值運(yùn)算與系統(tǒng)輸出相關(guān)操作技巧,需要的朋友可以參考下2017-09-09基于python內(nèi)置函數(shù)與匿名函數(shù)詳解
下面小編就為大家分享一篇基于python內(nèi)置函數(shù)與匿名函數(shù)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01