From: Ian Kelly on
On Wed, Jun 30, 2010 at 12:39 PM, Stef Mientki <stef.mientki(a)gmail.com> wrote:
> So I wonder why len is not allowed on None
> and if there are objections to extend the len function .

For the same reason that (None + 42) doesn't return 42, and that
(None.upper()) doesn't return NONE.
From: Emile van Sebille on
On 6/30/2010 11:39 AM Stef Mientki said...
> 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:

which works fine if beforehand you do

Result = presumedFuncCall() or []

particularly if you want to test len subsequently.

Emile


>
> if len ( Result )> 1 :
>
> But to prevent exceptions, i've to write ( I often forget)
> if Result and ( len ( Result )> 1 ) :
>
> So I wonder why len is not allowed on None
> and if there are objections to extend the len function .
>
> thanks,
> Stef Mientki
>
>


From: Steven D'Aprano on
Please pardon me for breaking threading, but Stef's original post has not
come through to me.

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.

Well there's your problem right there. If you have a function that
returns a list of X, and there are no X to return, you should return an
empty list, not None.

>>> filter(lambda n: n%2 == 0, [1, 3, 5, 7]) # return even numbers
[]


There are good use-cases for functions that sometimes return X and
sometimes return None, but they're rare.


--
Steven