Is there a way, given a simple Class, to output all the possible attributes for it? Standard attributes like __class__
and __doc__
and special read only attributes like __mro__
, __bases__
et al. Generally, all present attributes?
Considering the most simplistic case for a Class:
class myClass:
pass
The dir()
, vars()
and inspect.getmembers()
all exclude certain builtin
attributes. The most complete list is offered by using myClass.__dir__(MyClass)
which, while adding built in attributes, excludes user defined attributes of MyClass
, for example:
In [3]: set(MyClass.__dir__(MyClass)) - set(dir(MyClass))
Out[3]:
{'__abstractmethods__', '__base__', '__bases__',
'__basicsize__', '__call__', '__dictoffset__',
'__flags__', '__instancecheck__', '__itemsize__',
'__mro__', '__name__', '__prepare__', '__qualname__',
'__subclasscheck__', '__subclasses__', '__text_signature__',
'__weakrefoffset__', 'mro'}
According to one of the added similar questions, this is not possible. If presently still not possible, what is the rationale behind "hiding" certain attributes like __bases__
(from standard calls to dir(), vars() & inspect
and not ones like __name__
?
Similar Questions:
How to get a complete list of object's methods and attributes?
This is the most likely to be labeled as a duplicate, but, it is old and mostly regarding
Python 2.x
. The accepted answer is that there isn't a way but it was provided in 08'. The most recent answer in 12' suggestsdir()
for new style classes.Print all properties of a Python Class
Similar title, different content.
-
Offers
dir()
andinspect
solutions. Get all object attributes in Python?
Again, proposing
dir()
.