Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all attributes of a class in Python [duplicate]

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?

like image 993
Milano Avatar asked Oct 19 '22 07:10

Milano


1 Answers

Use vars

for property, value in vars(your_class).iteritems():
    print(property, ":", value)
like image 108
Himanshu Mishra Avatar answered Oct 21 '22 23:10

Himanshu Mishra