[Python-Dev] NoneType(None) raises exception
Steven D'Aprano
steve at pearwood.info
Fri Apr 26 02:27:59 CEST 2013
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?
But as soon as you think of it in general terms:
some_type(some_instance)
that's a pretty normal thing to do. And if it just so happened
that some_instance were an instance of some_type, 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
> 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.
--
Steven
More information about the Python-Dev
mailing list