Code redundancy

Andreas Löscher andreas.loescher at s2005.tu-chemnitz.de
Wed Apr 21 12:03:57 CEST 2010


You can do something like this:

>>> class A(): pass
>>> inst=A()
>>> exec("""
... a=1
... b=2
... c=3
... d=4
... """) in inst.__dict__
>>>  inst.a
1
>>>

This executes the Statement in the exec function and uses inst.__dict__
as namespace. But be aware, that this is not recommended. If you mess
with __dict__, you won't be able to replace it with some logic
(parameter) if you need to do something more than setting a variable.

Best






More information about the Python-list mailing list