How to “test” NoneType in python?

381    Asked by AmitSinha in Python , Asked on Apr 14, 2021

I have a method that sometimes returns a NoneType value. So how can I question a variable that is a NoneType? I need to use if method, for example

if not new:

new = '#'

I know that is the wrong way and I hope you understand what I meant.

Answered by Amit Sinha

As you want to check NoneType in python then you can use is an operator, like as follows:-

if variable is None:

if variable is not None:

Comparisons to singletons like None should always be done with is or is not, never the equality operators. This will help you solve python check if nonetype.



Your Answer

Interviews

Parent Categories