How to enumerate an object's properties in Python?

Jader Dias picture Jader Dias · Aug 9, 2009 · Viewed 159.2k times · Source

I C# we do it through reflection. In Javascript it is simple as:

for(var propertyName in objectName)
    var currentPropertyValue = objectName[propertyName];

How to do it in Python?

Answer

Georg Schölly picture Georg Schölly · Aug 9, 2009
for property, value in vars(theObject).items():
    print(property, ":", value)

Be aware that in some rare cases there's a __slots__ property, such classes often have no __dict__.