[Python-Dev] Replacing self.__dict__ in __init__

INADA Naoki songofacandy at gmail.com
Sun Mar 25 21:43:45 EDT 2018


>
> The dict can be replaced during __init__() and still get benefits of key-sharing.  That benefit is lost only when the instance dict keys are modified downstream from __init__().  So, from a dict size point of view, your optimization is fine.
>

I think replacing __dict__ lose key-sharing.


Python 3.6.4 (default, Mar  9 2018, 23:15:03)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class C:
...   def __init__(self, a, b, c):
...     self.a, self.b, self.c = a, b, c
...
>>> class D:
...   def __init__(self, a, b, c):
...     self.__dict__ = {'a':a, 'b':b, 'c':c}
...
>>> import sys
>>> sys.getsizeof(C(1,2,3).__dict__)
112
>>> sys.getsizeof(D(1,2,3).__dict__)
240


-- 
INADA Naoki  <songofacandy at gmail.com>


More information about the Python-Dev mailing list