June 2019
Beginner to intermediate
770 pages
19h 24m
English
The following are the six comparison operators and the special methods that implement them:
|
Method |
Operator |
|
object.__lt__(self, other) |
< |
|
object.__le__(self, other) |
<= |
|
object.__eq__(self, other) |
== |
|
object.__ne__(self, other) |
!= |
|
object.__gt__(self, other) |
> |
|
object.__ge__(self, other) |
>= |
The is operator compares object IDs. We can't meaningfully override this since it's independent of any specific class. The in comparison operator is implemented by object.__contains__(self, value). This isn't meaningful for numeric values.
Note that equality testing is a subtle business. Since floats are approximations, we have to be very careful to avoid direct equality ...
Read now
Unlock full access