Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #27161
| References | <502cebf4$0$6905$e4fe514c@news2.news.xs4all.nl> |
|---|---|
| Date | 2012-08-16 23:37 +1000 |
| Subject | Re: type(None)() |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3361.1345124274.4697.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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