Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the name of attribute in python object? [duplicate]

For example I have next python class

class Myclass():
     a = int
     b = int

Imagine that I don't know the name this class, so I need to get the names of attributes? ("a" and "b")

like image 791
megido Avatar asked Dec 28 '22 19:12

megido


1 Answers

If you want all (including private) attributes, just

dir(Myclass)

Attributes starting with _ are private/internal, though. For example, even your simple Myclass will have a __module__ and an empty __doc__ attribute. To filter these out, use

filter(lambda aname: not aname.startswith('_'), dir(Myclass))
like image 137
phihag Avatar answered Dec 31 '22 13:12

phihag