[Python-ideas] Augmented assignment syntax for objects.

Steven D'Aprano steve at pearwood.info
Thu Apr 27 19:34:11 EDT 2017


On Wed, Apr 26, 2017 at 08:02:39AM -0400, tritium-list at sdamon.com wrote:

> >     self.__dict__.update(kwargs)
> 
> Touching __dict__ feels dirty to me.


Indeed. The right way is:

    vars(self).update(kwargs)

although that doesn't work if self is written to use __slots__ instead 
of having a __dict__.

You can even (almost) get this same effect and still have named 
parameters:

    def __init__(self, fe, fi, fo, fum, spam, eggs, foo, bar, baz):
        vars(self).update(locals())
        del self.self


-- 
Steve


More information about the Python-ideas mailing list