returning True, False or None

Matteo Dell'Amico della at toglimi.linux.it
Mon Feb 7 16:23:39 EST 2005


nghoffma wrote:
> sorry, that should have been:
> 
> py>>import sets
> py>>def doit(thelist):
> ...     s = sets.Set(thelist)
> ...     if  s == sets.Set([None]):
> ...             return None
> ...     else:
> ...             return max(s - sets.Set([None]))
> 

Since a function that doesn't return is equivalent to one that returns 
None, you can write it as:

 >>> def doit(lst):
...     s = set(lst) - set([None])
...     if s: return max(s)

that looks to me as the most elegant so far, but this is just because 
it's mine :-)

You can also filter out Nones with a list/generator comprehension, but 
sets are just more elegant...

-- 
Ciao,
Matteo



More information about the Python-list mailing list