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

Don Jennings dfjennings at gmail.com
Sat Jul 27 21:14:52 CEST 2013


On Jul 27, 2013, at 3:00 PM, Jim Mooney wrote:

> 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.

In [20]: bool(L)
Out[20]: True

An empty list does evaluate to False, but a list which contains another list (even if the contained list happens to be empty) evaluates to True.

Take care,
Don



More information about the Tutor mailing list