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

Lion Chen chnlion79 at gmail.com
Fri May 4 16:21:40 CEST 2012


于 2012年05月04日 22:12, Dave Angel 写道:
> 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()
>
>
> i see, thanks  :)
Lion Chen
>



More information about the Tutor mailing list