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

Joel Goldstick joel.goldstick at gmail.com
Fri May 4 16:28:15 CEST 2012


On Fri, May 4, 2012 at 10:12 AM, Dave Angel <d at davea.name> wrote:
> 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
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

One more thing.  The reason the L is printed in the python interactive
shell is because it is showing the repr() of the variable.
Whe

-- 
Joel Goldstick


More information about the Tutor mailing list