getting all user defined attributes of a class

George Sakkis george.sakkis at
Thu Feb 7 20:49:16 CET 2008

On Feb 7, 1:42 pm, Amit Gupta <emaila... at> wrote:

> Thanks. What I found is: If I call iterate over the __dict__ of the
> instance of the class, I only get user-atttributes and not built-in
> attributes. I have an instance of that class, anyway, so this will do.
> However, I wonder if I am getting just lucky and this might change in
> future. In that regard the solution provides by all posters above
> might well be more robust.

Instances and classes have separate namespaces:

class X(object):
    x = 1
    def __init__(self):
        self.y = 2

>>> X().__dict__
{'y': 2}
>>> X.__dict__
<dictproxy object at 0xb7beab9c>

>>> X.__dict__.items()
[('__module__', '__main__'),
 ('__dict__', <attribute '__dict__' of 'X' objects>),
 ('x', 1),
 ('__weakref__', <attribute '__weakref__' of 'X' objects>),
 ('__doc__', None),
 ('__init__', <function __init__ at 0xb7b5a454>)]

And neither of those includes attributes defined in superclasses,
classes with __slots__,  pseudo-attributes through __getattr__ and
possibly more I've missed.


More information about the Python-list mailing list