Generic constructors and duplication of internal Python logic

John J. Lee jjl at pobox.com
Sat Apr 24 15:28:16 EDT 2004


Peter Otten <__peter__ at web.de> writes:

> Peter Otten wrote:
> 
> > But will I stop trying to come up with something better? no way. Here's my
> > next try, and I'm likely to present something completely different again
> > if you don't like it - not sure if that's a promise or a threat :-)
> 
> A threat.
> 
> def setLocals(d, selfName="self"):
>     self = d.pop(selfName)
>     for n, v in d.iteritems():
>         setattr(self, n, v)
> 
> class Demo(object):
>     def __init__(self, foo, bar, baz=2, bang=3):
>         setLocals(locals())

Perfect!  About a hundred times better than my solution.  Maybe this
should be a Python Cookbook entry?


John



More information about the Python-list mailing list