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

John Machin sjmachin at lexicon.net
Tue Apr 21 05:26:58 CEST 2009


On Apr 20, 6:54 pm, Gerhard Häring <g... at ghaering.de> 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.
> I prefer to write it explicitly:
> if len(lst) > 0:
>     ...
> if item is None:
>     ...
> etc.

Consider this snippet:

while stack and some_condition(stack[-1]):
  # do stuff including popping and breaking
else:
  # raise an exception

Would you use an explicit len(stack) > 0 ?



More information about the Python-list mailing list