views:

1021

answers:

4

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?

+5  A: 
for property, value in vars(theObject).iteritems():
    print property, ": ", value

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

Georg
now how to change the value? in Javascript you could do: object[property] = newValue. How to do it in Python?
Jader Dias
I got it: objectName.__dict__[propertyName] = newValue
Jader Dias
use setattr() instead.
Nelson
+7  A: 

dir() is the simple way. See here:

Guide To Python Introspection

EBGreen
+5  A: 

The __dict__ property of the object is a dictionary of all its other defined properties. Note that Python classes can override __getattr__ and make things that look like properties but are not in __dict__. There's also the builtin functions vars() and dir() which are different in subtle ways. And __slots__ can replace __dict__ in some unusual classes.

Objects are complicated in Python. __dict__ is the right place to start for reflection-style programming. dir() is the place to start if you're hacking around in an interactive shell.

Nelson
+12  A: 

See inspect.getmembers(object[, predicate]).

Return all the members of an object in a list of (name, value) pairs sorted by name. If the optional predicate argument is supplied, only members for which the predicate returns a true value are included.

>>> [name for name,thing in inspect.getmembers([])]
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', 
'__delslice__',    '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', 
'__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', 
'__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__','__reduce_ex__', 
'__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', 
'__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 
'insert', 'pop', 'remove', 'reverse', 'sort']
>>>
gimel
+1, inspect is the right way to tackle this kind of tasks!
Alex Martelli
Yeah, this answer is great; never used this module before. getmembers() is implemented by just walking the results of dir(object), btw.
Nelson
Can you elaborate why this is better than accessing __dict__? The Python documentation is less than helpful, especially because normally it uses the term `attributes` instead of `members` (is there any difference between the two?). They could have fixed the name in Python 3.0 to make it consistent.
nikow
Oops, meant `__dict__`, sorry.
nikow
@nikow: inspect.getmembers() is guaranteed to keep working even if the internal details change.
Georg
Could someone clarify, does inspect.getmembers() differ fundamentally from dir(), or is it just a convenient wrapper around dir (which the "Note:" in its documentation implies might be the case)?
Nicholas Knight