returning True, False or None

Steven Bethard steven.bethard at gmail.com
Mon Feb 7 16:35:35 EST 2005


Matteo Dell'Amico wrote:
> 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 :-)

Cool.  I prefer to be explicit about returns (e.g. not counting on the 
automatic return None), and I'd rather not create the unnecessary None 
set, so I would probably write this like:

py> def f(lst):
...     s = set(lst)
...     s.discard(None)
...     if s:
...         return max(s)
...     else:
...         return None
...

But it's definitely a very elegant solution.  Thanks!

Steve



More information about the Python-list mailing list