Question about None

Steven D'Aprano steve at REMOVETHIS.cybersource.com.au
Sat Jun 13 02:29:34 EDT 2009


Paul LaFollette wrote:

> 3) (this is purely philosophical but I am curious)  Would it not be
> more intuitive if
> isinstance(None, <anything at all>) returned true?

Good grief no!!!

None is an object. It has a type, NoneType. It's *not* a string, or a float,
or an int, or a list, so why would you want isinstance() to *lie* and say
that it is?


Python is good for testing these sorts of ideas:

>>> _isinstance = isinstance
>>> def isinstance(obj, type):
...     if obj is None: return True
...     return _isinstance(obj, type)
...
>>> x = "parrot"
>>> if isinstance(x, str):
...     print x.upper() 
...
PARROT
>>> x = None
>>> if isinstance(x, str): print x.upper()
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'upper'




-- 
Steven




More information about the Python-list mailing list