3 ms·
It would be a type error because that would be helpful. Since comparing a bytes to a str is not usually what you want, Python could make you be explicit about i
by minitech 8y ago
It would be a type error because that would be helpful. Since comparing a bytes to a str is not usually what you want, Python could make you be explicit about it. It’s too late now, and it might have been considered another backwards compatibility break (so a good thing to keep out of Python 3), but it’s not a bad idea on its own. Heterogeneous collections are pretty rare.
Also, `==` doesn’t always answer the question of whether two things are equivalent in Python:
>>> 1.0 == 1
True
>>> {'a': 5} == OrderedDict({'a': 5})
True
(OrderedDict equality isn’t transitive.)
- gvx 8y agoPretty rare but not unheard of. I do know that most of the code bases I work in would require significant reworking if I have to expect `==` to throw a TypeError for basic builtin types.
- guitarbill 8y agoNot quite with ==, but there is precedence where operators with basic builtin types throw a TypeError when the operation doesn't make sense: >>> 0 > None Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '>' not supported between instances of 'int' and 'NoneType'
- wool_gather 8y agoAbsolutely; there's no good boolean answer for > when you have different types, so the `TypeError` is perfect there.
- ubernostrum 8y agoAt the same time, this result is entirely dependent on the types involved. For example, "1.0 > 0" has a well-defined answer, and Python returns it. Considering the expression "x > y", the full process is: 1. Call x.__gt__(y). 2. If that returns a bool, that's the result. If it raises an exception, propagate the exception. 3. If it returns the sentinel value NotImplemented, attempt the reflected version of the operation: y.__lt__(x). 4. If that returns a bool, that's the result. If it raises an exception, propagate the exception. 5. If it returns the sentinel value NotImplemented, raise TypeError and inform the programmer this operator is not valid on these types.
- uryga 8y agoCould you explain how your example shows that?
- minitech 8y agoWhich “that”? If you’re asking about `OrderedDict`, I’m referring to equality between `OrderedDict`s: >>> a = OrderedDict([(1, 2), (3, 4)]) >>> b = {1: 2, 3: 4} >>> c = OrderedDict(reversed(a.items())) >>> a == b == c True >>> a == c False
- uryga 8y agoI was referring to this: > Also, `==` doesn’t always answer the question of whether two things are equivalent because the examples you posteda kinda make it look like it does, i.e. `1.0 == 1` even though one is a float and the other is an int.