On Fri, May 8, 2020 at 1:06 PM Steven D'Aprano <st...@pearwood.info> wrote:

> Whatever we might feel about equality and identity in the wider
> philosophical sense, in the *Python programming sense* the semantic
> meaning of the two operators are orthogonal:
>
> * some equal objects are not identical;
> * and some identical objects are not equal.
>

You yourself introduced—speculatively—the idea of another equality
operator, .EQ., that would be "equal in some sense not captured by '=='.  I
just posted another comment where I gave function names for six plausibly
useful concepts of "equality" ... well, technically,
equivalence-for-purpose.

The distinction you make seems both pedantic and factually wrong.  More
flat-footed still is "equal objects are ones whose .__eq__() method returns
something truthy."  It doesn't actually need to define any of the behaviors
we think of as equality/equivalence.  I was going to write a silly example
of e.g. throwing a random() into the operation, but I don't think I have to
for the point to be obvious.

Both '==' and 'is' are ways of saying equivalent-for-a-purpose.  For that
matter, so is math.isclose() or numpy.allclose().  Or those json-diff
libraries someone just linked to.  Given that different Python
implementations will give different answers for 'some_int is
some_other_int' where they are "equal" in an ordinary sense, identity isn't
anything that special in most cases.  Strings are likewise sometimes cached
(but differently by version and implementation).

The only cases where identity REALLY has semantics I would want to rely on
are singletons like None and True, and I guess for custom mutable objects
when you want to make sure which state is separated versus shared. Well,
OK, I guess lists are an example of that already for the same reason.

For non-singleton immutables, identity is not really a meaningful thing.  I
mean, other than in a debugger or code profiler, or something special like
that. I honestly do not know whether, e.g. '(1, "a", 3.5) is (1, "a",
3.5)'.  I'll go try it, but I won't be sure the answer for every
implementation, version, and even runtime, whether that answer will be
consistent.


-- 
The dead increasingly dominate and strangle both the living and the
not-yet born.  Vampiric capital and undead corporate persons abuse
the lives and control the thoughts of homo faber. Ideas, once born,
become abortifacients against new conceptions.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/7REWSEZP5NIG2P3HBCTIHC55GC7CU4MV/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to