Home > Article > Backend Development > How to get a list of class attributes in Python
This article mainly introduces how to get the list of class attributes in Python. The article introduces it in detail through example code. I believe it has certain reference value for everyone's study or work. Friends in need can refer to it. For reference, let’s take a look below.
Preface
Recently I encountered a need at work to obtain the static attributes of a class, that is to say, there is a class Type, I To dynamically obtain the value of Type.FTE
.
There are two simplest solutions:
getattr(Type, 'FTE') Type.__dict__['FTE']
So, if you want to get a list of class attributes, what should you do?
The first thing to come on stage is dir, which can return a list of all attribute names in the current scope:
>>> 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']
can be matched Use the functions in the inspect package to filter:
>>> [i for i in dir(list) if inspect.isbuiltin(getattr(list, i))] ['__new__', '__subclasshook__']
The inspect package also contains:
>>> [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']
It can also be used with 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']
As mentioned above, __dict__ can also be used to get the attribute list:
>>> 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__']
For more related articles on how to get the list of class attributes in Python, please pay attention to the PHP Chinese website!