handling many default values

Michele Simionato michele.simionato at gmail.com
Tue Nov 14 03:26:16 EST 2006


Gabriel Genellina wrote:
> At Monday 13/11/2006 13:33, Michele Simionato wrote:
>
> >Alan Isaac wrote:
> > > Also, as an aside, no one objected to using
> > >     self.__dict__.update(kwargs)
> > > in the __init__ function of the parameter holding class.
> >
> >It is a common trick, also shown in the Python cookbook, IIRC. If you
> >are anal about
> >double underscores, you can also use
> >
> >vars(self).update(kwargs)
>
> Not good - that relies on vars() returning the actual __dict__.
>  From <http://docs.python.org/lib/built-in-funcs.html>:
> "The returned dictionary should not be modified: the effects on the
> corresponding symbol table are undefined."

I believe the issue is with classes, where vars(klass) returns a
dictproxy which is read only.
In the case of instances (i.e. the OP' case) vars(self) returns the
instance dictionary and
my suggestion is correct. However you was right to point out this
subtility, it is always
worth to know.

           Michele Simionato




More information about the Python-list mailing list