[Python-Dev] NoneType(None) raises exception (original) (raw)

MRAB python at mrabarnett.plus.com
Fri Apr 26 03:12:56 CEST 2013


On 26/04/2013 01:27, Steven D'Aprano wrote:

On 26/04/13 09:56, MRAB wrote:

On the one hand, NoneType(None) seems a strange thing to do. Only when you write it out like that as constants. It's no more, or less, strange than str('spam') or int(1) or list([]). Why would you do that? None is a singleton, but instances of str, int, list, etc aren't. Why can it take an argument when there's only ever one of them?

That's why it seems strange to me.

But as soon as you think of it in general terms:

sometype(someinstance) that's a pretty normal thing to do. And if it just so happened that someinstance were an instance of sometype, it would be surprising if the call failed. (I initially wrote "astonishing", but then I realised that some types take more than one argument, e.g. FunctionType. So it's merely surprising.)

On the other hand:

type(value)(value) == value would return True for the built-in types (will certain exceptions, such as when value is float("NaN")). Not an exception, that works fine in 3.3:

value = float('nan') type(value)(value) nan But:

value = float('NaN') type(value)(value) == value False

That's what I mean by it being an "exception".

Let's ask the Zen:

Special cases aren't special enough to break the rules. Although practicality beats purity. I cannot think of any use-case where I would actively want NoneType(None) to fail. That would be like having bool(True) raise an exception. On the other hand, NoneType(x) for any other x ought to fail. OK, so practicality (or pragmatism) wins.



More information about the Python-Dev mailing list