cosmologicon / pywat

Python wats

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Iterable types in comparisons is wrong

messense opened this issue · comments

>>> a = [0, 0]
>>> (x, y) = a
>>> (x, y) == a
False
>>> type(a)
list
>>> type((x, y))
tuple

>>> [1,2,3] == sorted([1,2,3])
True
>>> (1,2,3) == sorted((1,2,3))
False
>>> type(sorted((1, 2, 3))
list

You are comparing different data types, that makes it unequal.

>>> a = (0, 0)
>>> (x, y) = a
>>> (x, y) == a
True

>>> [1,2,3] == sorted([1,2,3])
True
>>> [1,2,3] == sorted((1,2,3))
True
>>> (1,2,3) == tuple(sorted((1,2,3)))
True

Yes. That's the issue. But why does that make it unsurprising? There are plenty of cases where different data types compare equal:

>>> a = [0]
>>> set(a) == frozenset(a)
True
>>> type(set(a)) == type(frozenset(a))
False
>>> True == 1 == 1.0
True