type(None)()

H

Hans Mulder

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
 
L

Laszlo Nagy

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()
 
C

Chris Angelico

Why doesn't it just return an existing instance of the type,
like bool, int, str and other built-in non-mutable types do?

With int and str, it's only an optimization, and not guaranteed to happen.
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:
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
 
I

Ian Kelly

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.
 
S

Steven D'Aprano

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.
 
R

Robert Kern

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
 
M

MRAB

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 += 1

Of course, an alternative would be:

counts = defaultdict(lambda: 0)
 
S

Stefan Behnel

Steven D'Aprano, 16.08.2012 15:58:
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.None

Stefan
 
S

Stefan Behnel

Ramchandra Apte, 16.08.2012 17:39:
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
 
E

Ethan Furman

Hans said:
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~
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top