[Tutor] True and 1 [was Re: use of the newer dict types]

Jim Mooney cybervigilante at gmail.com
Sat Jul 27 21:00:00 CEST 2013


On 27 July 2013 11:36, Steven D'Aprano <steve at pearwood.info> wrote:

> However, this will test your knowledge:
>
> L = []
> L.append(L)
> [L] == L
>
> True or false? Can you explain why?

I hate it when you make me think.

Um, they both contain an empty list. Although the first one contains
an empty list that contains an empty list, and the two fail an 'is'
test. They are pointing to different things. But they test okay for
equivalence since they both contain an empty object, which resolves to
False, I think. It's getting a bit deep, here ;')

Another thing that hit me is that although you can compare unequal types, as in
if [True] == True: print(True) , which works:

If you make a nonequivalence comparison, the program crashes:

if [True] > True: print(True)
Crash!

So although they are both comparisons, they are a different type of
comparison entirely.

Jim


More information about the Tutor mailing list