Asked 4 years ago
12 Aug 2020
Views 5610
steave ray

steave ray posted

Python : How to check NoneType in python?

i am getting error sometime when paython variable have value : NoneType .

i want to check if it is NoneType than need to do some sutff

if var not NoneType:
    do something



steave

steave
answered Aug 14 '20 02:15
Updated Sep 24 '21 02:13

you can use isinstance function to check type with current variable.

following code can help you better

if isinstance(var, type(None)):
            vard= ""
        else :
            vard=var

ruby-rails

ruby-rails
answered Sep 23 '21 00:00

use is operator to check weather variable is None or not




python=None
if python is None:
	print("None Type")
else:
	print("not a None Type")


it will print "None Type"





python=123
if python is None:
	print("None Type")
else:
	print("not a None Type")


it will print "not a None Type"


python=''
if python is None:
	print("None Type")
else:
	print("not a None Type")

in Python '' empty string does not means None
so it will print "not a None Type"
Post Answer