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


Groups > comp.lang.python > #10475

Re: NoneType and new instances

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ethan@stoneleaf.us>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'constructor': 0.07; 'skip:` 10': 0.07; '(actually': 0.09; 'bool': 0.09; 'false,': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'received:gator410.hostgator.com': 0.09; 'value:': 0.09; '~ethan~': 0.09; 'am,': 0.13; 'wrote:': 0.15; '9:39': 0.16; 'dictionaries': 0.16; 'grrr.': 0.16; 'instantiate': 0.16; 'lambda': 0.16; 'null.': 0.16; 'received:72.11': 0.16; 'received:72.11.125': 0.16; 'received:72.11.125.166': 0.16; 'cc:addr:python-list': 0.16; 'this:': 0.16; 'have:': 0.19; 'cc:2**0': 0.21; 'header:In-Reply-To:1': 0.22; 'dictionary': 0.23; "shouldn't": 0.23; 'somewhere': 0.23; 'code': 0.24; "i'm": 0.27; 'thu,': 0.28; 'cc:addr:python.org': 0.30; 'kelly': 0.30; 'looks': 0.30; 'values': 0.31; 'unable': 0.31; "skip:' 10": 0.32; "i'll": 0.33; 'header:User-Agent:1': 0.34; 'there': 0.34; 'be.': 0.34; 'describe': 0.34; 'things': 0.34; 'none': 0.35; 'like:': 0.35; 'subject:new': 0.36; 'some': 0.37; 'but': 0.37; 'using': 0.37; 'subject:: ': 0.38; 'portion': 0.38; 'returning': 0.38; 'else': 0.38; 'two': 0.38; 'should': 0.39; 'except': 0.39; 'might': 0.39; 'happen': 0.62; 'received:websitewelcome.com': 0.65; 'ever': 0.65; 'received:184': 0.67; 'special': 0.67; 'glad': 0.71; 'received:67.18.82': 0.84; 'received:gateway14.websitewelcome.com': 0.84; 'them:': 0.84
Date Thu, 28 Jul 2011 14:03:11 -0700
From Ethan Furman <ethan@stoneleaf.us>
User-Agent Thunderbird 1.5.0.10 (Windows/20070221)
MIME-Version 1.0
To Ian Kelly <ian.g.kelly@gmail.com>
Subject Re: NoneType and new instances
References <4E3182C3.9040306@stoneleaf.us> <CALwzidmRwWqS-nY3==8XetBMe2cS7DRikZ6OWOwbQ61w99X9tg@mail.gmail.com>
In-Reply-To <CALwzidmRwWqS-nY3==8XetBMe2cS7DRikZ6OWOwbQ61w99X9tg@mail.gmail.com>
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - gator410.hostgator.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - stoneleaf.us
X-BWhitelist no
X-Source
X-Source-Args
X-Source-Dir
X-Source-Sender mail.admailinc.com ([192.168.10.136]) [72.11.125.166]:2252
X-Source-Auth ethan+stoneleaf.us
X-Email-Count 1
X-Source-Cap dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ==
Cc Python <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1587.1311886063.1164.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1311886063 news.xs4all.nl 23872 [2001:888:2000:d::a6]:48241
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:10475

Show key headers only | 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