Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #27157 > unrolled thread
| Started by | Hans Mulder <hansmu@xs4all.nl> |
|---|---|
| First post | 2012-08-16 14:47 +0200 |
| Last post | 2012-08-16 09:07 -0700 |
| Articles | 11 — 9 participants |
Back to article view | Back to comp.lang.python
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
| From | Hans Mulder <hansmu@xs4all.nl> |
|---|---|
| Date | 2012-08-16 14:47 +0200 |
| Subject | type(None)() |
| Message-ID | <502cebf4$0$6905$e4fe514c@news2.news.xs4all.nl> |
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? > py> type(False)() is False > True Just wondering, -- HansM
[toc] | [next] | [standalone]
| From | Laszlo Nagy <gandalf@shopzeus.com> |
|---|---|
| Date | 2012-08-16 14:54 +0200 |
| Message-ID | <mailman.3360.1345122462.4697.python-list@python.org> |
| In reply to | #27157 |
On 2012-08-16 14:47, Hans Mulder 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?
Because None is a singleton. It is the only instance of its class. This
is very useful because it allows you to write conditions like this:
if obj is None:
do_something()
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2012-08-16 23:37 +1000 |
| Message-ID | <mailman.3361.1345124274.4697.python-list@python.org> |
| In reply to | #27157 |
On Thu, Aug 16, 2012 at 10:47 PM, Hans Mulder <hansmu@xs4all.nl> wrote:
> Why doesn't it just return an existing instance of the type,
> like bool, int, str and other built-in non-mutable types do?
>
>> py> type(False)() is False
>> True
With int and str, it's only an optimization, and not guaranteed to happen.
>>> a=int("1234")
>>> a is int("1234")
False
>>> a=str(1234)
>>> a is str(1234)
False
But with bool, it's required, as a means of "casting to boolean". With
True/False/None, it's normal to compare them with is:
>>> a=bool("1")
>>> a is bool("2")
True
So bool() has to return one of those two actual objects, and not an equivalent.
(Note: All examples done in CPython 3.2's IDLE on Windows. Other
environments, Pythons, versions, etc, may affect exactly what these
show.)
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2012-08-16 07:56 -0600 |
| Message-ID | <mailman.3362.1345125395.4697.python-list@python.org> |
| In reply to | #27157 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-08-16 13:58 +0000 |
| Message-ID | <502cfc78$0$29978$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #27157 |
On Thu, 16 Aug 2012 14:47:47 +0200, Hans Mulder 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? bool must return an instance, because it is designed to cast objects to a boolean. Since (by design) True and False are singletons (doubletons?), bool(x) will always return a pre-existing instance. Other built-in immutable types do not promise to do that. For example: py> a = float(42) py> b = float(42) py> a is b False Sometimes int and str will cache their instances, but this is an implementation detail subject to change without notice from version to version. None, NotImplemented and Ellipsis are singletons, but unlikely bool, there is no common use-case for having their types return the singleton instance. The standard design pattern for singletons is to raise an exception if you try to create an instance, so they do. However, this behaviour really only makes sense for singletons that hold state. (If they hold state, you might be tempted to change that state, not realising that you are changing a singleton and not a second instance.) In my opinion, this is a PITA for None and better behaviour would be to return the pre-existing NoneType instance, but I didn't design the language. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Stefan Behnel <stefan_ml@behnel.de> |
|---|---|
| Date | 2012-08-16 17:31 +0200 |
| Message-ID | <mailman.3369.1345131098.4697.python-list@python.org> |
| In reply to | #27164 |
Steven D'Aprano, 16.08.2012 15:58: >>> NoneType raises an error if you try to create a second instance. > In my opinion, this is a PITA for None and better behaviour would be to > return the pre-existing NoneType instance, but I didn't design the > language. The time machine strikes again. Python 3.3.0b1 (default:f7b59e890e30, Aug 11 2012, 05:30:10) [GCC 4.6.3] on linux Type "help", "copyright", "credits" or "license" for more information. >>> type(None)() >>> print(type(None)()) None Stefan
[toc] | [prev] | [next] | [standalone]
| From | Stefan Behnel <stefan_ml@behnel.de> |
|---|---|
| Date | 2012-08-16 17:44 +0200 |
| Message-ID | <mailman.3373.1345131902.4697.python-list@python.org> |
| In reply to | #27164 |
Ramchandra Apte, 16.08.2012 17:39: > On 16 August 2012 21:01, Stefan Behnel wrote: >> Steven D'Aprano, 16.08.2012 15:58: >>>>> NoneType raises an error if you try to create a second instance. >>> In my opinion, this is a PITA for None and better behaviour would be to >>> return the pre-existing NoneType instance, but I didn't design the >>> language. >> >> The time machine strikes again. >> >> Python 3.3.0b1 (default:f7b59e890e30, Aug 11 2012, 05:30:10) >> [GCC 4.6.3] on linux >> Type "help", "copyright", "credits" or "license" for more information. >>>>> type(None)() >>>>> print(type(None)()) >> None > > Are they the same object Obviously. None is a singleton (as was already mentioned a couple of times in this thread). Stefan
[toc] | [prev] | [next] | [standalone]
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2012-08-16 09:06 -0700 |
| Message-ID | <mailman.3375.1345132886.4697.python-list@python.org> |
| In reply to | #27164 |
Ramchandra Apte wrote: > Are they the same object Yes.
[toc] | [prev] | [next] | [standalone]
| From | Robert Kern <robert.kern@gmail.com> |
|---|---|
| Date | 2012-08-16 15:56 +0100 |
| Message-ID | <mailman.3366.1345129024.4697.python-list@python.org> |
| In reply to | #27157 |
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()? -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2012-08-16 16:13 +0100 |
| Message-ID | <mailman.3367.1345130003.4697.python-list@python.org> |
| In reply to | #27157 |
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)
[toc] | [prev] | [next] | [standalone]
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2012-08-16 09:07 -0700 |
| Message-ID | <mailman.3376.1345133476.4697.python-list@python.org> |
| In reply to | #27157 |
Hans Mulder 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? An oversight, and until a few months ago nobody had complained loud enough. ;) > Why doesn't it just return an existing instance of the type, > like bool, int, str and other built-in non-mutable types do? In 3.3 it now does. ~Ethan~
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web