Generic constructors and duplication of internal Python logic

Peter Otten __peter__ at web.de
Thu Apr 29 12:31:01 EDT 2004


John J. Lee wrote:

>> Peter Otten wrote:

>> 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?

Did that:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/280381

Peter



More information about the Python-list mailing list