Pyrex - __richcmp__
Tim Peters
tim_one at email.msn.com
Sat May 3 12:58:37 EDT 2003
[Haris Bogdanovic]
> How would look operator overloading for operator == :
>
> def __richcmp__(self, x, ???)
The six infix comparison operators can each be overloaded separately. To
overload only ==, define
def __eq__(self, other):
# return true or false, depending on whether you think
# self is equal to other
Note that you need to overload each one you intend to use! There are no
assumed relationships among them. For example, if you define __eq__, it
won't be called if the comparison operator is != (there's no assumption that
"x != y" gives the same result as "not x == y", so if you want to affect the
result of !=, you have to supply a __ne__ method too).
More information about the Python-list
mailing list