In Python you have the None singleton, which acts pretty oddly in certain circumstances:
>>> a = None
>>> type(a)
<type 'NoneType'>
>>> isinstance(a,None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types
So first off, <type 'NoneType'> displays that None is not a type, but that NoneType is. Yet when you run isinstance(a,NoneType), it responds with an error: NameError: name 'NoneType' is not defined
Now, given this, if you have a function with an input default set to None, and need to check, you would do the following:
if variable is None:
#do something
else:
#do something
what is the reason that I cannot do the following instead:
if isinstance(variable,None): #or NoneType
#do something
else:
#do something
I am just looking for a detailed explanation so I can better understand this
Edit: good application
Lets say I wanted to use isinstance so that I can do something if variable is a variety of types, including None:
if isinstance(variable,(None,str,float)):
#do something
if variable == Noneis anti-idiomatic Python. The standard way to do this test is by making use of the fact thatNoneis a singleton: useif variable is Noneinstead.