On Tue, May 05, 2020 at 09:34:28AM -0000, jdveiga@gmail.com wrote:
`(frozenset() == set()) is True` shocked me.
According to wikipedia https://en.wikipedia.org/wiki/Equality_(mathematics): "equality is a relationship between two quantities or, more generally two mathematical expressions, asserting that the quantities have the same value, or that the expressions represent the same mathematical object."
If lists and tuples are considered different "mathematical objects" (different types), they cannot be considered equal --tough they can be equivalent, for instance `([1, 2, 3] == list((1, 2, 3)) and tuple([1, 2, 3]) == (1, 2, 3)) is True`.
There is no good correspondence between "mathematical objects" and types. Even in mathematics, it is not clear whether the integer 1 as the same mathematical object as the real number 1, or the complex number 1, or the quaternion 1. In Python, we usually say that if a type is part of the numeric tower ABC, then instances with the same numeric value should be considered equal even if they have different types. But that's not a hard rule, just a guideline. And it certainly shouldn't be used as a precedent implying that non-numeric values should behave the same way. If you are looking for a single overriding consistant principle for equality in Python, I think you are going to be disappointed. Python does not pretend to be a formal mathematically consistent language and the only principle for equality in Python is that equality means whatever the object's `__eq__` method wants it to mean.
I can only explain `(frozenset() == set()) is True` vs `(list() == tuple()) is False` if:
a) `frozenset`s and `set`s are considered the same "mathematical objects". So immutability vs mutability is not a relevant feature in Python equality context. Then, `list() == tuple()` should be `True` if no other feature distinguishes lists from tuples, I suppose...
List and tuple are distinguished by the most important feature of all: the designer's intent. Tuples are records or structs, not frozen lists, which is why they are called tuple not frozen list :-) even if people use them as a defacto frozen list. On the other hand, frozensets are frozen sets, which is why they compare equal. Does this make 100% perfectly logical sense? Probably not. But it doesn't have to. Lists and tuples are considered to be independent kinds of thing, while sets and frozensets are considered to be fundamentally the same kind of thing differentiated by mutability. (In hindsight, it might have been more logically clear if mutable sets inherited from immutable frozensets, but we missed the chance to do that.) -- Steven