[Tutor] assign all parameters of __init__ to class variables?

Peter Otten __peter__ at web.de
Wed Nov 2 14:37:47 CET 2011


Alex Hall wrote:

> I have a class which takes a large number of optional arguments for
> its __init__. Instead of going through every single one and assigning
> it to "self.[name]", is there some quick way to take all the
> parameters of the constructor and assign them all to self.[name] in
> one step?

You could use something like

http://code.activestate.com/recipes/280381-clean-up-__init__-methods-that-
contain-only-attrib/

or

def __init__(self, **kw):
    self.__dict__.update(kw)

but I'd recommend against it. I've never used the recipe myself and instead 
try hard to keep argument list sizes managable.




More information about the Tutor mailing list