[Python-Dev] About [].append == [].append

Steven D'Aprano steve at pearwood.info
Thu Jun 21 09:39:56 EDT 2018


On Thu, Jun 21, 2018 at 02:33:27PM +0300, Ivan Pozdeev via Python-Dev wrote:

> First, tell us what problem you're solving.

You might not be aware of the context of Jereon's question. He is the 
author of PEP 579 and 580, so I expect he's looking into implementation 
details of the CPython builtin functions and methods.

https://www.python.org/dev/peps/pep-0579/

https://www.python.org/dev/peps/pep-0580/


> Strictly speaking, bound methods don't have an unambiguous notion of 
> equality:
> 
> are they equal if they do the same thing, or of they do they same thing 
> _on the same object_?

That's a red-herring, because CPython already defines an unambiguous 
notion of method equality. The problem is that the notion depends on 
whether the method is written in Python or not, and that seems like a 
needless difference.


> The result that you're seeing is a consequence of that same dichotomy in 
> the minds of the .__eq__ designers, and Python Zen advises "In the face 
> of ambiguity, refuse the temptation to guess." -- which is what you're 
> suggesting.

How do you come to that conclusion? If "refuse the temptation to guess" 
applied here, we couldn't do this:

py> "".upper == "".upper
True

(by your reasoning, it should raise an exception).

Note the contrast in treatment of strings with:

py> [].append == [].append
False

(The reason is that "" is cached and reused, and the empty string is 
not.)


> On 21.06.2018 14:25, Jeroen Demeyer wrote:
[...]

> >I think that comparing using "==" is the right thing to do because 
> >"is" is really an implementation detail.

+1


-- 
Steve


More information about the Python-Dev mailing list