3 ms·
It's not unambiguous at all. If I do "część" == "część".encode('<some-obscure-encoding>') should it return True? They are 'equivalent' after all. But that
by uryga 8y ago
It's not unambiguous at all. If I do
"część" == "część".encode('<some-obscure-encoding>')
should it return True? They are 'equivalent' after all. But that would mean it should work for any encoding, so testing `s == b` would entail trying to decode `b` as every known encoding to see if any of them gives `s`.
My point is, `s == b` is not a well-defined question. It only makes sense to ask it for a particular encoding: "Are these two things isomorphic according to cp-1252?"¹². Giving a type error is a decent way of signalling all this.
---
¹ And even then, I don't like automatic type conversions that enable things like `3 == "3"`, but I guess that's a matter of taste.
² Of course you could pick a convention like `s == b <=> s = b.decode("utf8")`, but that's a different question.
- wool_gather 8y agoNo, it should return `False` because they're different types, as I said. > I don't like automatic type conversions that enable things like `3 == "3"` I didn't mention this, and Python, the language under discussion, doesn't do this.
- uryga 8y agoOkay, it looks like I misunderstood your original point: > "Are these two things equivalent?" has an unambiguous answer regardless of whether the things are of different types. as "Isomorphic things should compare equal" So, to respond to (what I think is) your actual point: in my view, equality is defined on values of a single type, so I prefer to distinguish "a isn't equal to b" (= False) and "there's no defined way to compare a and b, because they have different types" (= some kind of error or a null-like value) To me, conflating the two seems counterproductive, but perhaps this is personal preference (probably correlated with how much one likes static typing).
- ubernostrum 8y agoPython has operator overloading, though, so a flat rule that comparisons involving values of different types must always be a type error would actually reduce the power of the language. Granted, there aren't a ton of use cases where it's important to be able to do this, but it is useful on occasion.
- deleted 8y ago[deleted]
- uryga 8y agoYou're right – I guess I got too hung up on the types. What I meant to say is that I prefer to distinguish "`a` is not equal to `b`" and "equality between `a` and `b` is undefined"; the typing aspect is kind of orthogonal and muddles the discussion. (I guess I ended up conflating equality and equivalence[1], but in my defense, most languages seem to do that too; and the presence of the `is` operator in Python mixes things up even more) [1] https://en.m.wikipedia.org/wiki/Equality_(mathematics)#Relation_with_equivalence_and_isomorphism https://en.m.wikipedia.org/wiki/Equality_(mathematics)#Relat...