Expression can be simplified on list

Ned Batchelder ned at nedbatchelder.com
Mon Sep 12 22:33:27 EDT 2016


On Monday, September 12, 2016 at 5:21:51 PM UTC-4, Thomas 'PointedEars' Lahn wrote:
> Chris Angelico wrote:
> 
> > On Mon, Sep 12, 2016 at 9:14 PM, Daiyue Weng <daiyueweng at gmail.com> wrote:
> > If you know that 'errors' is always going to be a list, you can check
> > for emptiness thus:
> > 
> > if not errors:
> 
> If I knew that it is always going to be a list or a tuple, I would check its 
> length instead:
> 
>   if len(errors) == 0:
> 
> Only if I did not know that I would use the “not” keyword, because the 
> latter
> 
>   a) forces a type conversion to “bool”;
>   b) also works with values of other types.

Why do you object to the type conversion to bool?

--Ned.



More information about the Python-list mailing list