3 ms·
On a tangential note, no pun intended, I am a little confused. Why would you ever want compare in the manner of "TrickyAngle(6) < TrickyAngle(5)"? I can under
by snake_plissken 11y ago
On a tangential note, no pun intended, I am a little confused. Why would you ever want compare in the manner of "TrickyAngle(6) < TrickyAngle(5)"? I can understand something like "trickyAngle_A.degrees < trickAngle_b.degrees" but when comparing instances of a class like that, when would one ever be...greater than the other?
- angelbob 11y agoOften, an instance of a class represents a mathematical entity (vector, matrix, imaginary number, mathematical set, et al) where less than might be a fundamentally reasonable operation, especially in some specific context. His example is using it as an angle, where comparing two of them is conceptually pretty sensible. If you pretend it's a structure, sure, you'll need to extract primitive data types out of it before doing anything with it. But if you want it to behave as a higher-level abstraction, reimplementing less-than (say, for comparing the vector magnitude) at every call site isn't great.
- philh 11y ago(This is completely beside the point.) > comparing two of them is conceptually pretty sensible. Well, there are a bunch of sensible ways to do it, depending what the angle represents. You might not want to privilege any of them over the others directly in the class. Is it a point on the unit circle? It doesn't make much sense to compare (sqrt(2), sqrt(2)) to (-1, 0). Is it an angular distance to travel from point A to point B? Then you want 355 and -5 to both be less than 10. Is it an amount that a screw has to be rotated? Then you can't take mods, so 720 > 360 > 0. Do you have a robot that can only turn anticlockwise? Then maybe 5 < -10 < 355. This is the comparison implied by the implementation in TFA (self.degrees = degrees % 360), but it's pretty obscure.
- deleted 11y ago[deleted]
- exogen 11y ago1. Encapsulation (arguably the whole purpose of creating such a class in the first place). 2. Would you think it's reasonable for a class to implement equality ==, so it can tell you if it's equal to another instance of the same class? Then why not ordering? 3. Take Python's set type as a useful example. A < B tells you whether A is a subset of B, A | B returns the union, etc. All very handy.
- cballard 11y agoI disagree with 3, a subset is not "less than" the superset. Sets are equatable, but they're not comparable. A named function or a different operator would be preferable.
- dragonwriter 11y agoThe set of sets is, itself, a strictly partially ordered set by the "is a proper subset of" relation (and a non-strict partially ordered set by the "is a subset of" relation); if Python's < and > operators are viewed as generic strict ordering operators (and those symbols are, outside of Python, conventionally used for both strict total orders like those on reals and strict partial orders), then their use with sets for the strict partial order defined by the "is a proper subset of" relation is quite natural.