[Python-Dev] Replacing self.__dict__ in __init__

Kirill Balunov kirillbalunov at gmail.com
Sat Mar 24 11:15:09 EDT 2018


2018-03-24 17:18 GMT+03:00 Tin Tvrtković <tinchester at gmail.com>:
>
> I've found that, if a class has more than one attribute, instead of
> creating an init like this:
>
>     self.a = a
>     self.b = b
>     self.c = c
>
> it's faster to do this:
>
>     self.__dict__ = {'a': a, 'b': b, 'c': c}
>
> i.e. to replace the instance dictionary altogether. On PyPy, their core
> devs inform me this is a bad idea because the instance dictionary is
> special there, so we won't be doing this on PyPy.
>

But why you need to replace it? When you can just update it:

class C:
    def __init__(self, a, b, c):
        self.__dict__.update({'a': a, 'b': b, 'c': c})

I'm certainly not a developer. Just out of curiosity.

With kind regards,
-gdg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180324/5b1cd185/attachment.html>


More information about the Python-Dev mailing list