create boolean

Grant Edwards grante at visi.com
Sat Mar 7 00:03:08 EST 2009


On 2009-03-07, Rhodri James <rhodri at wildebst.demon.co.uk> wrote:
> On Fri, 06 Mar 2009 15:34:08 -0000, Grant Edwards <invalid at invalid> wrote:
>
>> 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 != [])
>
> The second comparison isn't actually necessary, since an
> empty list is True and a non-empty one False.
>
>    b = (n is not None) and n
>
> Putting the comparison in does make the code slightly less
> "magic", though, so it's not a bad idea to do it!

Putting in the second comparison in makes the code match the
stated requirement.  Otherwise you have to start making
assumptions about what n might be besides None or the empty
list.

-- 
Grant




More information about the Python-list mailing list