Let\'s say I create an instance of a class and want to assign some values to its public properties. Usually, this would be done like this:
class MyClass:
Using dir with setattr should do the job
class MyClass:
def __init__(self):
self.name = None
self.text = None
myclass = MyClass()
myclass.name = 'My name'
for prop in dir(myclass):
print '%s:%s'%(prop,getattr(myclass,prop))
print
for prop in dir(myclass):
if prop[:2]!='__' and prop[-2:]!='__':
print prop[-2:]
setattr(myclass,prop,"Foo Bar")
for prop in dir(myclass):
print '%s:%s'%(prop,getattr(myclass,prop))
But be careful because this code also sets '__doc__', '__init__', '__module__' properties to "Foo Bar". So you will have to take care of avoiding certain things given to you by dir (especially those which start and end with __ double underscores).