Hwy doesn't len(None) return zero ?

Steven D'Aprano steve-REMOVE-THIS at cybersource.com.au
Wed Jun 30 18:23:34 EDT 2010


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



More information about the Python-list mailing list