NoneType and new instances

Ethan Furman ethan at
Thu Jul 28 17:03:11 EDT 2011

Ian Kelly wrote:
> On Thu, Jul 28, 2011 at 9:39 AM, Ethan Furman <ethan at> wrote:
>> Why is NoneType unable to produce a None instance?  I realise that None is a
>> singleton, but so are True and False, and bool is able to handle returning
>> them:
> The bool constructor works (actually just returns one of the existing
> singletons) so that you can do things like this:
> truth_value = bool(x + 5)
> return my_dict[truth_value]
> Why would you ever need to instantiate NoneType?

I'm glad you asked!  I'm using dictionaries to describe fields and what 
their return values should be.  There happen to be two special cases: 
empty and Null.  So a portion of the dictionary looks like:

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

Depending on the needs of the user, `some_func` might be `str`, `int`, 
`custom_class`, or `NoneType`.  Somewhere else in the code I have:

     if not value: # or some such
         cls = fielddef['empty']
         return cls()

This works for *everything* except NoneType.  grrr.  ;)


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?

More information about the Python-list mailing list