Hwy doesn't len(None) return zero ?

Stephen Hansen me+list/python at ixokai.io
Wed Jun 30 14:50:22 EDT 2010


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.


> So I wonder why len is not allowed on None
> and if there are objections to extend the len function .

Len is not allowed on None, becaues None is not a sequence, and doesn't 
have a length. None, *very* much on purpose, is distinct and does not 
behave like anything else. It's the "I'm not anything" object.

-- 

    ... Stephen Hansen
    ... Also: Ixokai
    ... Mail: me+list/python (AT) ixokai (DOT) io
    ... Blog: http://meh.ixokai.io/




More information about the Python-list mailing list