Hello,

this question is not directly related to PyPy but to general Python
semantics. I'm posting it here since I know there are very knowledgeable
people here and honestly this is the friendliest list for questions like
this.

I'm working on optimizing __init__s that the attrs library generates. My
question is, is there anything fundamentally wrong with having an __init__
like this:

def __init__(self, a, b, c):
    self.__dict__ = {'a': b, 'b': b, 'c': c}

(basically throwing away the existing instance dict and replacing it with a
fresh one). As far as I can see, this is actually faster on CPython if
there is more than one attribute, and about the same speed on PyPy
(benchmarking on PyPy is kinda hard, the difference is 0.2 ns vs 0.22 ns,
not sure 0.02 ns is a significant difference). And this approach works for
frozen classes (an attrs feature) too.

It's just that doing it this way is unconventional and a little scary.
Would we be violating a Python rule somewhere and making stuff blow up
later if we went this way?
_______________________________________________
pypy-dev mailing list
pypy-dev@python.org
https://mail.python.org/mailman/listinfo/pypy-dev

Reply via email to