How to get attribute name in python object?

For example, I have the following python class

class Myclass():
     a = int
     b = int

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

+3
source share
1 answer

If you want all (including private) attributes, just

dir(Myclass)

Attributes starting with _are private / internal. For example, even your simple one Myclasswill have __module__an empty attribute __doc__. To filter them, use

filter(lambda aname: not aname.startswith('_'), dir(Myclass))
+9
source

All Articles