NoneType and new instances

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Jul 28 21:29:39 EDT 2011


Chris Angelico wrote:

> On Fri, Jul 29, 2011 at 7:03 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
>> I'll use a lambda to get around it, but that's not very elegant.  Why
>> shouldn't NoneType be able to return the singleton None?
> 
> Why a lambda?
> 
> def ThisFunctionWillReturnNone():
>     pass

This is a good use-case for a lambda. Ethan's use-case is a dict of
constructors:

     fielddef = { 'empty':some_func, 'null':some_func }

There's no need to expose the constructor outside of the dict, hence:

     fielddef = { 'empty': str, 'null': lambda: None }

does the job. No need for a top-level named function.


-- 
Steven




More information about the Python-list mailing list