and [True,True] --> [True, True]?????

Peter Otten __peter__ at web.de
Mon Apr 20 05:17:22 EDT 2009


Gerhard Häring wrote:

> Peter Otten wrote:
>> bdb112 wrote:
>> 
>>> Your explanation of Boolean ops on lists was clear.
>>> It leads to some intriguing results:
>>>
>>> bool([False])
>>> --> True
>>>
>>> I wonder if python 3 changes any of this?
>> 
>> No. Tests like
>> 
>> if items:
>>    ...
>> 
>> to verify that items is a non-empty list are a widespread idiom in
>> Python. They rely on the behaviour you observe.
> 
> Are they widespread? I haven't noticed, yet.

That is my impression.
 
> I prefer to write it explicitly:
> 
> if len(lst) > 0:
>     ...

Using google codesearch I get

matches      search expression
ca. 1000     lang:python "if items:"
216          lang:python "if len(items) > 0:"

This could of course mean that "people who like 'items' as a list name also
like the 'if items:...' idiom" or "'items' is a popular name for boolean
values" or "the search result is spammed by a gazillion zope versions"...

Peter



More information about the Python-list mailing list