change NoneType, NotImplementedType, & ellipses to return the appropriate singleton

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 I hit this issue when I had a dictionary of types to instantiate, one of which was NoneType. Is there any reason why we shouldn't change these three to produce the singleton instance instead of raising an error? Pro: They would then be consistent with every other built-in which will return either a new instance (int, str, float, etc), or the singleton instance that already exists (bool, and sometimes int & str), as well as no longer needing the `lambda:None` workaround (or worse, a try-except if you don't know what types are coming in). Con: Might be code that relies on the exception being raised -- is anybody aware of any? ~Ethan~

Frankly, type(None) only exists in the language because all objects have a type, and have to be introspectable in a uniform manner. This is occasionally useful to introspection code, but of no practical consequence for most users. Using type(None) as a cheap alternative to lambda:None strikes me as an invitation for unreadable code -- since type(None) doesn't have much of a practical purpose, users have no expectation of what it would do (even though you can reason it out). --Guido On Sun, Aug 7, 2011 at 9:07 AM, Devin Jeanpierre <jeanpierreda@gmail.com> wrote:
-- --Guido van Rossum (python.org/~guido)

On 2011-08-07, at 18:37 , David Townshend wrote:
You're suggesting that the *value* could be callable (and return itself), the proposal is about *types*, and them becoming "normal" (callable) types: currently, if you get a hold of NoneType, NotImplementedType or ellipsis (generally via a call to `type`) you can not use it to get an instance of that class back: >>> type(None)() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot create 'NoneType' instances >>> type(NotImplemented)() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot create 'NotImplementedType' instances >>> type(...)() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot create 'ellipsis' instances This proposal is merely about removing this forbidding. Booleans don't have this issue: `bool` is already a normal (callable) type, and returns `False` when called without arguments.

Ah, sorry - I didn't read it properly! On Aug 7, 2011 6:46 PM, "Masklinn" <masklinn@masklinn.net> wrote: the proposal is about *types*, and them becoming "normal" (callable) types: currently, if you get a hold of NoneType, NotImplementedType or ellipsis (generally via a call to `type`) you can not use it to get an instance of that class back:
type, and returns `False` when called without arguments.

That looks pretty reasonable to me, and, more importantly, would increase consistency as you point out :) (I have had a similar issue in the past, where I expected this to work but it didn't -- so I replaced NoneType with lambda: None.) cheers lvh

On 7/29/2011 12:37 PM, Ethan Furman wrote:
Is there any reason why we shouldn't change these three to produce the singleton instance instead of raising an error?
You have all the approvals you need (congrats), so file a feature request and mention this thread or even better, add a iink mail.python.org/<whatever)

Ethan Furman <ethan@stoneleaf.us> writes:
You have all the approvals you need (congrats), so file a feature request […]
Congratulations, and thanks for taking this through the right channels. -- \ “It is undesirable to believe a proposition when there is no | `\ ground whatever for supposing it true.” —Bertrand Russell, _The | _o__) Value of Scepticism_, 1928 | Ben Finney

Frankly, type(None) only exists in the language because all objects have a type, and have to be introspectable in a uniform manner. This is occasionally useful to introspection code, but of no practical consequence for most users. Using type(None) as a cheap alternative to lambda:None strikes me as an invitation for unreadable code -- since type(None) doesn't have much of a practical purpose, users have no expectation of what it would do (even though you can reason it out). --Guido On Sun, Aug 7, 2011 at 9:07 AM, Devin Jeanpierre <jeanpierreda@gmail.com> wrote:
-- --Guido van Rossum (python.org/~guido)

On 2011-08-07, at 18:37 , David Townshend wrote:
You're suggesting that the *value* could be callable (and return itself), the proposal is about *types*, and them becoming "normal" (callable) types: currently, if you get a hold of NoneType, NotImplementedType or ellipsis (generally via a call to `type`) you can not use it to get an instance of that class back: >>> type(None)() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot create 'NoneType' instances >>> type(NotImplemented)() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot create 'NotImplementedType' instances >>> type(...)() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot create 'ellipsis' instances This proposal is merely about removing this forbidding. Booleans don't have this issue: `bool` is already a normal (callable) type, and returns `False` when called without arguments.

Ah, sorry - I didn't read it properly! On Aug 7, 2011 6:46 PM, "Masklinn" <masklinn@masklinn.net> wrote: the proposal is about *types*, and them becoming "normal" (callable) types: currently, if you get a hold of NoneType, NotImplementedType or ellipsis (generally via a call to `type`) you can not use it to get an instance of that class back:
type, and returns `False` when called without arguments.

That looks pretty reasonable to me, and, more importantly, would increase consistency as you point out :) (I have had a similar issue in the past, where I expected this to work but it didn't -- so I replaced NoneType with lambda: None.) cheers lvh

On 7/29/2011 12:37 PM, Ethan Furman wrote:
Is there any reason why we shouldn't change these three to produce the singleton instance instead of raising an error?
You have all the approvals you need (congrats), so file a feature request and mention this thread or even better, add a iink mail.python.org/<whatever)

Ethan Furman <ethan@stoneleaf.us> writes:
You have all the approvals you need (congrats), so file a feature request […]
Congratulations, and thanks for taking this through the right channels. -- \ “It is undesirable to believe a proposition when there is no | `\ ground whatever for supposing it true.” —Bertrand Russell, _The | _o__) Value of Scepticism_, 1928 | Ben Finney
participants (15)
-
Antoine Pitrou
-
Ben Finney
-
dag.odenhall@gmail.com
-
David Townshend
-
Devin Jeanpierre
-
Ethan Furman
-
Georg Brandl
-
Guido van Rossum
-
Laurens Van Houtven
-
Masklinn
-
MRAB
-
Raymond Hettinger
-
Stefan Behnel
-
Steven D'Aprano
-
Terry Reedy