Hwy doesn't len(None) return zero ?
Dave Angel
davea at ieee.org
Wed Jun 30 15:18:22 EDT 2010
Stephen Hansen wrote:
> On 6/30/10 11:39 AM, Stef Mientki wrote:
>> hello,
>>
>> I've lot of functions that returns their result in some kind of tuple /
>> list / array,
>> and if there is no result, these functions return None.
>> Now I'm often what to do something if I've more than 1 element in the
>> result.
>> So I test:
>>
>> if len ( Result ) > 1 :
>>
>> But to prevent exceptions, i've to write ( I often forget)
>> if Result and ( len ( Result ) > 1 ) :
>
> Just do:
>
> if Result:
>
> You don't have to do a length check > 1; because if Result has a
> length of 0, it'll be false too. So the above check will catch both
> None, and empty sequences.
> <snip>
Look closer: the OP wanted len(Result) > 1 not len(Result) > 0.
For that, you need two checks, for example, as for example:
if Result and (len(Result)>1):
.
DaveA
More information about the Python-list
mailing list