create boolean

Rhodri James rhodri at wildebst.demon.co.uk
Sun Mar 8 19:33:12 EDT 2009


On Sat, 07 Mar 2009 05:03:08 -0000, Grant Edwards <grante at visi.com> wrote:

> 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.

The OP stated that we *could* assume that n was None or a
list, so I stand by what I said.

-- 
Rhodri James *-* Wildebeeste Herder to the Masses



More information about the Python-list mailing list