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


Groups > comp.lang.python > #27169

Re: type(None)()

Date 2012-08-16 16:13 +0100
From MRAB <python@mrabarnett.plus.com>
Subject Re: type(None)()
References <502cebf4$0$6905$e4fe514c@news2.news.xs4all.nl> <CALwzidkyeXZBPqrXJqisyYkhcs3P_tamc22Njf5k9ckHZcxqCA@mail.gmail.com> <k0j1nl$8j5$1@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.3367.1345130003.4697.python-list@python.org> (permalink)

Show all headers | 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