NoneType and new instances

B

Billy Mays

<class 'NoneType'>
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
TypeError: cannot create 'NoneType' instances

Why is NoneType unable to produce a None instance? I realise that None
is a singleton, but so are True and False, and bool is able to handle
returning them:

--> bool(0) is bool(0)
True

This feels like a violation of 'Special cases aren't special enough to
break the rules.'

~Ethan~


Probably for the same reason Ellipsis and NotImplemented also can't be
instantiated. What that reason is I don't know. Related:

http://bugs.python.org/issue6477#msg90641
 
E

Ethan Furman

Consider:

Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
--> for ins in ({0:'0'}, (1,), set([2, 3]), [4, 5], 6, 'seven',
.... 8.0, True, None):
.... print(type(ins))
.... type(ins)()
....
<class 'dict'>
{}
<class 'tuple'>
()
<class 'set'>
set()
<class 'list'>
[]
<class 'int'>
0
<class 'str'>
''
<class 'float'>
0.0
<class 'bool'>
False
<class 'NoneType'>
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
TypeError: cannot create 'NoneType' instances

Why is NoneType unable to produce a None instance? I realise that None
is a singleton, but so are True and False, and bool is able to handle
returning them:

--> bool(0) is bool(0)
True

This feels like a violation of 'Special cases aren't special enough to
break the rules.'

~Ethan~
 
E

Ethan Furman

Ben said:
That answers your question. Because None is a singleton, the job of its
type is to make sure there are no other instances.

Which it can do quite easily by returning the single instance of None --
it is not necessary to raise an exception to fulfill its duty.

Well, they don't meet the definition of a singleton, because there are
two instances of ‘bool’ :)

Okay, a slightly relaxed definition of singleton, since there is only
ever one instance with the value of True, or the value of False;
likewise, there is only ever one instance with the value of None.

In the case of ‘bool’, the rule was broken before being introduced.

I think we disagree on what the rule is. I see it as "Return an
instance if you can." Nobody has yet pointed out a good reason on why
NoneType, NotImplementedType, and ellipsis (to be thorough ;) cannot or
should not return the single instance that exists, when every other
built-in will return either a new object, or the single object that
exists for that value.

~Ethan~
 
S

Steven D'Aprano

Ethan said:
Why is NoneType unable to produce a None instance? I realise that None
is a singleton, but so are True and False, and bool is able to handle
returning them:

I've asked this question myself. As I recall the answer, it's just a matter
of personal preference. Some people consider that singletons should raise
an error if you try to instantiate them a second time. I think that's
the "standard" behaviour referenced in the Gang Of Four design patterns
book, and it's certainly very common in Java. Others think that it should
just return the same instance.

The advantage of raising an error is that if the singleton holds state, then
the caller won't be fooled into thinking they got a second instance. They
have to explicitly refer to the one instance each time. But since None
doesn't hold state, there is no advantage here.

As for True and False, bool has to be able to return them, because the whole
purpose of exposing bool is so people can call it: if bool(some_value) was
an error, that would defeat the purpose of having bool!
 
G

Gregory Ewing

Steven said:
As for True and False, bool has to be able to return them, because the whole
purpose of exposing bool is so people can call it: if bool(some_value) was
an error, that would defeat the purpose of having bool!

Bool is different, because it doubles as a function for
coercing things to bool. There's no corresponding concept
of coercing something to None, or Ellipsis, etc.
 
T

Terry Reedy

Ethan's proposal was accepted on python-ideas. In Python 3.3, the
classes for the singletons None, Ellipsis, and NotImplemented will be
callables that return the singleton.

It turns out that the reason an exception is now raised is that there
currently is no .__new__ method, so an exception is raised by default.
..__new__ methods returning the singleton have been added.

Terry Jan Reedy
 
P

python

Hi Terry,
Ethan's proposal was accepted on python-ideas. In Python 3.3, the classes for the singletons None, Ellipsis, and NotImplemented will be callables that return the singleton.

Thanks for sharing this news. Its motivating to see this type of
feedback loop because it encourages others (myself included!) to become
more involved in the process.

Malcolm
 
B

bruno.desthuilliers

--> bool(0) is bool(0)
True

This test is not reliable - a same id can be reused for terms (I have
already seen such things happening). If you want a reliable test, use:

#> a = bool(0)
#> b = bool(0)
#> a is b
True

Note that this still fails to prove anything since bool is a subclass
of int and CPython caches "small" integers:

#> a = 42
#> b = 42
#> a is b
True
 
T

Terry Reedy

This test is not reliable

It is in the sense that it will always work -- because False/True are
doubletone constants and so documented.

But expr is expr == True does not reliably say it will always be true,
because
 
S

Steven D'Aprano

Terry said:
It is in the sense that it will always work -- because False/True are
doubletone constants and so documented.

Surely that should be doubleton :)

2.2: True and False released as names for 1 and 0; bool was a built-in
function, but not a type.

2.3: bool promoted to a type; True and False actual bools. The documentation
claims that there's only one instance of each.

I'm sure that I remember a short period during which Python had bools, but
didn't guarantee that there would only be one instance of True and one of
False, but I'm damned if I can find any evidence of this. Am I
confabulating?
 

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,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top