NoneType and new instances
steve+comp.lang.python at pearwood.info
Fri Jul 29 03:16:56 CEST 2011
Ethan Furman wrote:
> 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!
More information about the Python-list