0

Suppose a variable is defined:

a=None


On running below code:

print(isinstance(a,NoneType))

gave error: NameError: name 'NoneType' is not defined

For any other data type, its working correctly

print(isinstance(5,int), isinstance(4.0,float), isinstance(False,bool), isinstance("on_and_on",str), isinstance([1,"2",3.0,True,None],list), isinstance((1,"2",3.0,True,None),tuple), isinstance({4,1,3,2},set), isinstance({1:"a","apple":2,5.0:True},dict), isinstance(5-2j,complex))

Why?

  • The issue isn't `isinstance` at all, that function never even gets called because there is a `NameError`, because `NoneType` is not a variable that is defined anywhere. The linked duplicate was bad, but this is definitely a duplicate. I've changed the target to a more appropriate duplicate – juanpa.arrivillaga Jun 17 '19 at 16:27

1 Answers1

1

you need to declare the type:

isinstance(a, type(None))
### result:
True

or just use a bool check:

a=None

if a is None:
    print("a is NoneType")

### result:
a is None type

see also this post if you're interested in some theory.

cccnrc
  • 1,195
  • 11
  • 27