[Tutor] why i < j is True, j < k is False

Dave Angel d at davea.name
Fri May 4 16:12:56 CEST 2012


On 05/04/2012 09:29 AM, Lion Chen wrote:
> Hi, All,
> here are the codes:
>
> class a:
> pass
>
>
> i = a ()
> j = a ()
> k = a ()
>
> i < j returns True
>
> j < k returns False
>
> why?
>
> Lion Chen
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

i, j and k are three distinct instances of the class a  (which should be
capitalized for clarity).

Since you don't supply any comparison operator special methods in your
class, Python 2 simply compares their id values.  So the ordering is
entirely undefined, and each of us might get different results. I, for
example, got true and true.

In Python 3, you'd get the following error, which is an improvement, imho:

TypeError: unorderable types: A() < A()




-- 

DaveA



More information about the Tutor mailing list