type(None)()

Chris Angelico rosuav at gmail.com
Thu Aug 16 09:37:50 EDT 2012


On Thu, Aug 16, 2012 at 10:47 PM, Hans Mulder <hansmu at 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



More information about the Python-list mailing list