[pypy-dev] Safety of replacing the instance dict

Armin Rigo armin.rigo at gmail.com
Tue Jan 30 15:37:34 EST 2018


Hi,

On 30 January 2018 at 13:13, Antonio Cuni <anto.cuni at gmail.com> wrote:
> sidenote: if you do the following, you can replace the __dict__ without
> incurring into performance penalties (Armin, please correct me if I'm
> wrong):
>
> import __pypy__
> def __init__(self):
>     self.__dict__ = __pypy__.newdict('instance')

This is a no-op, which takes time to execute but indeed doesn't seem
to disable the optimization.  However I don't see the point.  You may
as well write it like that:

   def __init__(self, x, y, z):
        d = self.__dict__
        d['x'] = x
        d['y'] = y
        d['z'] = z

It is a bit slower than "self.x = x; self.y = y; self.z = z" in PyPy
but doesn't throw away the optimization.  On CPython it is probably
faster.


Armin


More information about the pypy-dev mailing list