[BangPypers] Python comparisons
Noufal Ibrahim KV
noufal at nibrahim.net.in
Sat Dec 5 02:26:18 EST 2015
So I came across this today..
>>> class Number(object):
... def __init__(self, n):
... self.n = n
...
>>> m = Number(10)
>>> n = Number(5)
>>>
>>> m < n
True
This is documented like so
> If no __cmp__(), __eq__() or __ne__() operation is defined, class
> instances are compared by object identity (“address”).
over here https://docs.python.org/2/reference/datamodel.html#object.__cmp__
It seems a rather arbitrary thing to do. Why is it implemented at all?
I'd expect it to just break with a TypeError similar to what would
happen if I do
>>> m + n
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'Number' and 'Number'
>>>
--
Cordially,
Noufal
http://nibrahim.net.in
More information about the BangPypers
mailing list