Re: [BangPypers] Python comparisons
On 5 December 2015 at 12:56, Noufal Ibrahim KVwrote: > > > 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__ Maybe because that was the historical way that C did it? I agree that the Python 3 exception makes more sense. Regards, Gora ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Python comparisons
On Sat, Dec 05 2015, Gora Mohanty wrote: [...] > Maybe because that was the historical way that C did it? I agree that > the Python 3 exception makes more sense. [...] We can only guess but you'll have to explicitly code this in and I can't think of any situation where it would make sense. -- Cordially, Noufal http://nibrahim.net.in ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Python comparisons
On Sat, Dec 05 2015, Anand Chitipothu wrote: [...] > Thats why you should use Python 3. Here is what you get with Python 3. [...] Yet another reason to move. I'm still curious why they did it the other way in 2.x though. -- Cordially, Noufal http://nibrahim.net.in ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Python comparisons
On Sat, Dec 5, 2015 at 12:56 PM, Noufal Ibrahim KVwrote: > > 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 "", line 1, in > TypeError: unsupported operand type(s) for +: 'Number' and 'Number' > >>> > Thats why you should use Python 3. Here is what you get with Python 3. >>> class Foo: pass ... >>> a = Foo() >>> b = Foo() >>> >>> a < b Traceback (most recent call last): File "", line 1, in TypeError: unorderable types: Foo() < Foo() That would give True in Python 2. Anand ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
[BangPypers] Python comparisons
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 "", line 1, in TypeError: unsupported operand type(s) for +: 'Number' and 'Number' >>> -- Cordially, Noufal http://nibrahim.net.in ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers