6

Is it possible to get all attributes of some class or object? I have a class with many attributes and I want to make a function/method which returns all attributes of object equals to None.

Since there is many of these attributes I have to avoid writing all of them into the function.

Something like this:

def get_all_empty_attributes(self):
    for attr  in self.attributes.names:
        if self.attr==None:
            yield attr

Is it possible to do using builtin functions?

Milano
  • 18,048
  • 37
  • 153
  • 353
  • You can, as in the answer below but perhaps you should consider using a named tuple or just a dict if you have lots of key-value pairs which you want to iterate over and query dynamically instead of explicit attributes. – pvg Jul 12 '15 at 13:12

1 Answers1

8

Use vars

for property, value in vars(your_class).iteritems():
    print(property, ":", value)
Himanshu Mishra
  • 8,510
  • 12
  • 37
  • 74