python get only class attribute no superclasses

Extract from the python documentation

A class has a namespace implemented by a dictionary object. Class
attribute references are translated to lookups in this dictionary,
e.g., C.x is translated to C.__dict__[“x”] (although for new-style
classes in particular there are a number of hooks which allow for
other means of locating attributes). When the attribute name is not
found there, the attribute search continues in the base classes

In other words, __dict__ contains only “local” attributes of the class, the superclass’s attributes are stored in the superclass __dict__.

So, you can use __class__.__dict__.iteritems() to retrieve only the class attributes.

On Python 3 you should use __class__.__dict__.items().

Leave a Comment