Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #10475

Re: NoneType and new instances

Date 2011-07-28 14:03 -0700
From Ethan Furman <ethan@stoneleaf.us>
Subject Re: NoneType and new instances
References <4E3182C3.9040306@stoneleaf.us> <CALwzidmRwWqS-nY3==8XetBMe2cS7DRikZ6OWOwbQ61w99X9tg@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1587.1311886063.1164.python-list@python.org> (permalink)

Show all headers | View raw


Ian Kelly wrote:
> On Thu, Jul 28, 2011 at 9:39 AM, Ethan Furman <ethan@stoneleaf.us> 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.  ;)

~Ethan~

PS
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?

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: NoneType and new instances Ethan Furman <ethan@stoneleaf.us> - 2011-07-28 14:03 -0700

csiph-web