Code redundancy

Alan Harris-Reid aharrisreid at googlemail.com
Wed Apr 21 13:26:50 CEST 2010


Andreas Löscher wrote:
> You can do something like this:
>
>   
>>>> class A(): pass
>>>> inst=)
>>>> exec("""
>>>>         
> ... a=
> ... 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
Hi Andreas, thanks for the reply,

Looks like I'll be sticking with repeating the class-name for a while.

Regards,
Alan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100421/0b4e29b6/attachment.html>


More information about the Python-list mailing list