G
Greg
Hello,
This is my first post here, so if this is not the correct place to ask
this, please direct me to the best place.
In looking at the py3k documentation for comparing two classes, two
different view points are expressed (at least it seems so to me).
1) At http://docs.python.org/py3k/reference/datamodel.html:
"There are no implied relationships among the comparison operators.
The truth of x==y does not imply that x!=y is false. Accordingly, when
defining __eq__(), one should also define __ne__()..."
-- This seems to support the view that if in our code, we would like
to use comparison operators <, >, =, !=, etc. then we should define a
__lt__(), __gt__(), __eq__(), __ne__(), etc. for each comparison
operator we would like.
This appears to contrast
2) At http://docs.python.org/py3k/library/stdtypes.html:
"Instances of a class cannot be ordered with respect to other
instances of the same class, or other types of object, unless the
class defines enough of the methods __lt__(), __le__(), __gt__(), and
__ge__() (in general, __lt__() and __eq__() are sufficient, if you
want the conventional meanings of the comparison operators)."
-- This seems to imply that to get all of the operators, only
__lt__() and __eq__() need to be defined (just __lt__() should suffice
though I thought).
So, which is it supposed to be? Or am I reading the documentation
wrong?
Thanks!
-Greg-
This is my first post here, so if this is not the correct place to ask
this, please direct me to the best place.
In looking at the py3k documentation for comparing two classes, two
different view points are expressed (at least it seems so to me).
1) At http://docs.python.org/py3k/reference/datamodel.html:
"There are no implied relationships among the comparison operators.
The truth of x==y does not imply that x!=y is false. Accordingly, when
defining __eq__(), one should also define __ne__()..."
-- This seems to support the view that if in our code, we would like
to use comparison operators <, >, =, !=, etc. then we should define a
__lt__(), __gt__(), __eq__(), __ne__(), etc. for each comparison
operator we would like.
This appears to contrast
2) At http://docs.python.org/py3k/library/stdtypes.html:
"Instances of a class cannot be ordered with respect to other
instances of the same class, or other types of object, unless the
class defines enough of the methods __lt__(), __le__(), __gt__(), and
__ge__() (in general, __lt__() and __eq__() are sufficient, if you
want the conventional meanings of the comparison operators)."
-- This seems to imply that to get all of the operators, only
__lt__() and __eq__() need to be defined (just __lt__() should suffice
though I thought).
So, which is it supposed to be? Or am I reading the documentation
wrong?
Thanks!
-Greg-