[Python-Dev] Updated PEP 509

Oscar Benjamin oscar.j.benjamin at gmail.com
Mon Apr 18 10:38:54 EDT 2016


On 18 April 2016 at 12:46, Jim J. Jewett <jimjjewett at gmail.com> wrote:
>>
>> * I removed the dict[key]=value; dict[key]=value. It's really a
>> micro-optimization. I also fear that Raymond will complain because it
>> adds an if in the hot code of dict, and the dict type is very
>> important for Python performance.
>
> That is an acceptable answer.  Though I really do prefer explicitly
> *refusing to promise* either way when the replacement/replaced objects
> are ==.
>
> dicts (and other collections) already assume sensible ==, even
> explicitly allowing self-matches of objects that are not equal to
> themselves.  I don't like the idea of making new promises that violate
> (or rely on violations of) that sensible == assumption.

dicts make assumptions about the behaviour of __eq__ for the *keys*
but not for the *values* (on which no assumptions are made). The only
way to replace a key in a dict with another equal key (having a
well-behaved hash function) is to pop the key out and then insert the
new key so it's not possible to replace a key with another equal key
without bumping the version twice. So presumably you're referring to
the values here right?

The purpose of the PEP is to be able to guard for changes to
namespaces which are implemented as dicts. So if
builtins.__dict__['abs'] is replaced by foo then we don't care what
foo.__eq__ says about the situation: any optimisation that assumed
builtins.abs was not monkeypatched is invalidated. That's why the
version update is needed. Without it the version cannot be relied upon
as an optimisation guard. Consider:

class MyAbs:
    def __eq__(self, other):
        return True
    def __call__(self, arg):
        return - arg

builtins.abs = MyAbs()

--
Oscar


More information about the Python-Dev mailing list