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


Groups > comp.lang.python > #27169

Re: type(None)()

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; '16,': 0.03; 'that?': 0.05; 'raises': 0.07; 'type,': 0.07; 'be:': 0.09; 'bool': 0.09; 'instance.': 0.09; 'subject:None': 0.09; 'typeerror:': 0.09; 'aug': 0.13; 'cases': 0.15; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'kern': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'subject:type': 0.16; 'wrote:': 0.17; 'instance': 0.17; 'thu,': 0.17; '>>>': 0.18; 'versions': 0.20; 'error.': 0.21; '"",': 0.22; 'int,': 0.22; 'simpler': 0.22; 'raise': 0.24; 'second': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; '(most': 0.27; 'am,': 0.27; "doesn't": 0.28; '>>>>': 0.29; "d'aprano": 0.29; 'received:192.168.1.3': 0.29; 'steven': 0.29; 'str': 0.29; 'that.': 0.30; 'unlike': 0.30; 'error': 0.30; 'file': 0.32; 'instances': 0.33; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'built-in': 0.35; 'robert': 0.35; 'pm,': 0.35; 'there': 0.35; 'two': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'header:Reply-To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'counts': 0.81; 'reply-to:addr:python.org': 0.84
X-CM-Score 0.00
X-CNFS-Analysis v=2.0 cv=W6e6pGqk c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=DKcI9XZsuF4A:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=OHDTbXam6hcA:10 a=5ks5QSfAXL4h_WluCpoA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117
X-AUTH mrabarnett:2500
Date Thu, 16 Aug 2012 16:13:20 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20120713 Thunderbird/14.0
MIME-Version 1.0
To python-list@python.org
Subject Re: type(None)()
References <502cebf4$0$6905$e4fe514c@news2.news.xs4all.nl> <CALwzidkyeXZBPqrXJqisyYkhcs3P_tamc22Njf5k9ckHZcxqCA@mail.gmail.com> <k0j1nl$8j5$1@ger.gmane.org>
In-Reply-To <k0j1nl$8j5$1@ger.gmane.org>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To python-list@python.org
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.3367.1345130003.4697.python-list@python.org> (permalink)
Lines 33
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1345130003 news.xs4all.nl 6945 [2001:888:2000:d::a6]:55626
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:27169

Show key headers only | View raw


On 16/08/2012 15:56, Robert Kern wrote:
> On 8/16/12 2:56 PM, Ian Kelly wrote:
>> On Thu, Aug 16, 2012 at 6:47 AM, Hans Mulder <hansmu@xs4all.nl> wrote:
>>> On 8/08/12 04:14:01, Steven D'Aprano wrote:
>>>> NoneType raises an error if you try to create a second instance. bool
>>>> just returns one of the two singletons (doubletons?) again.
>>>>
>>>> py> type(None)()
>>>> Traceback (most recent call last):
>>>>    File "<stdin>", line 1, in <module>
>>>> TypeError: cannot create 'NoneType' instances
>>>
>>> Why is that?
>>>
>>> Why doesn't it just return an existing instance of the type,
>>> like bool, int, str and other built-in non-mutable types do?
>>
>> Because unlike those other types there is no use case for that.  It's
>> simpler to raise an error.
>
> What are the use cases for the empty-argument versions of bool(), int(),
> float(), and str()?
>
They can be used with defaultdict. For example:

counts = defaultdict(int)
for i in items:
     counts[i] += 1

Of course, an alternative would be:

counts = defaultdict(lambda: 0)

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


Thread

type(None)() Hans Mulder <hansmu@xs4all.nl> - 2012-08-16 14:47 +0200
  Re: type(None)() Laszlo Nagy <gandalf@shopzeus.com> - 2012-08-16 14:54 +0200
  Re: type(None)() Chris Angelico <rosuav@gmail.com> - 2012-08-16 23:37 +1000
  Re: type(None)() Ian Kelly <ian.g.kelly@gmail.com> - 2012-08-16 07:56 -0600
  Re: type(None)() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-16 13:58 +0000
    Re: type(None)() Stefan Behnel <stefan_ml@behnel.de> - 2012-08-16 17:31 +0200
    Re: type(None)() Stefan Behnel <stefan_ml@behnel.de> - 2012-08-16 17:44 +0200
    Re: type(None)() Ethan Furman <ethan@stoneleaf.us> - 2012-08-16 09:06 -0700
  Re: type(None)() Robert Kern <robert.kern@gmail.com> - 2012-08-16 15:56 +0100
  Re: type(None)() MRAB <python@mrabarnett.plus.com> - 2012-08-16 16:13 +0100
  Re: type(None)() Ethan Furman <ethan@stoneleaf.us> - 2012-08-16 09:07 -0700

csiph-web