create boolean

Grant Edwards invalid at invalid
Fri Mar 6 10:34:08 EST 2009


On 2009-03-06, Fencer <no.spam at plz.ok> wrote:

> Hi, I need a boolean b to be true if the variable n is not
> None and not an empty list, otherwise b should be false.

> I ended up with:

> b = n is not None and not not n

I'd do it like this:

  b = (n is not None) and (n != [])

Your code doesn't meet your stated requirement.  Your code
incorrectly evaluates to False if n is any of the following:

 (,)
 {}
 False
 0
 0.0
 
Your stated requirement is for b to be True for all those cases. 

> which seems to work
>
> but is that normally how you would do it?
>
> It can be assumed that n is always None or a list that might
> be empty

Well then, that's different. :)

Still, I'd do it my way.  I don't like assumptions.
  
-- 
Grant Edwards                   grante             Yow! If Robert Di Niro
                                  at               assassinates Walter Slezak,
                               visi.com            will Jodie Foster marry
                                                   Bonzo??



More information about the Python-list mailing list