Getting attributes of a class Getting attributes of a class python python

Getting attributes of a class


Try the inspect module. getmembers and the various tests should be helpful.

EDIT:

For example,

class MyClass(object):    a = '12'    b = '34'    def myfunc(self):        return self.a>>> import inspect>>> inspect.getmembers(MyClass, lambda a:not(inspect.isroutine(a)))[('__class__', type), ('__dict__',  <dictproxy {'__dict__': <attribute '__dict__' of 'MyClass' objects>,   '__doc__': None,   '__module__': '__main__',   '__weakref__': <attribute '__weakref__' of 'MyClass' objects>,   'a': '34',   'b': '12',   'myfunc': <function __main__.myfunc>}>), ('__doc__', None), ('__module__', '__main__'), ('__weakref__', <attribute '__weakref__' of 'MyClass' objects>), ('a', '34'), ('b', '12')]

Now, the special methods and attributes get on my nerves- those can be dealt with in a number of ways, the easiest of which is just to filter based on name.

>>> attributes = inspect.getmembers(MyClass, lambda a:not(inspect.isroutine(a)))>>> [a for a in attributes if not(a[0].startswith('__') and a[0].endswith('__'))][('a', '34'), ('b', '12')]

...and the more complicated of which can include special attribute name checks or even metaclasses ;)


def props(cls):     return [i for i in cls.__dict__.keys() if i[:1] != '_']properties = props(MyClass)


myfunc is an attribute of MyClass. That's how it's found when you run:

myinstance = MyClass()myinstance.myfunc()

It looks for an attribute on myinstance named myfunc, doesn't find one, sees that myinstance is an instance of MyClass and looks it up there.

So the complete list of attributes for MyClass is:

>>> dir(MyClass)['__doc__', '__module__', 'a', 'b', 'myfunc']

(Note that I'm using dir just as a quick and easy way to list the members of the class: it should only be used in an exploratory fashion, not in production code)

If you only want particular attributes, you'll need to filter this list using some criteria, because __doc__, __module__, and myfunc aren't special in any way, they're attributes in exactly the same way that a and b are.

I've never used the inspect module referred to by Matt and Borealid, but from a brief link it looks like it has tests to help you do this, but you'll need to write your own predicate function, since it seems what you want is roughly the attributes that don't pass the isroutine test and don't start and end with two underscores.

Also note: by using class MyClass(): in Python 2.7 you're using the wildly out of date old-style classes. Unless you're doing so deliberately for compatibility with extremely old libraries, you should be instead defining your class as class MyClass(object):. In Python 3 there are no "old-style" classes, and this behaviour is the default. However, using newstyle classes will get you a lot more automatically defined attributes:

>>> class MyClass(object):        a = "12"        b = "34"        def myfunc(self):            return self.a>>> dir(MyClass)['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a', 'b', 'myfunc']