Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

None Python error/bug?

Tags:

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
like image 988
Ryan Saxe Avatar asked Jun 19 '13 18:06

Ryan Saxe


People also ask

How do you fix a None problem in Python?

Also, if run==True in the loop header, it cannot be False on the next line. The print functions in your input statements return None , and that's why you see None before the user input. Remove the calls to print() , input will print the text anyway.

Why is None instead of == None?

None is a singleton object (there only ever exists one None ). is checks to see if the object is the same object, while == just checks if they are equivalent. But since there is only one None , they will always be the same, and is will return True.

Is None and 0 same in Python?

None is not the same as 0, False, or an empty string. None is a data type of its own (NoneType) and only None can be None.

How do you check if a value is None Python?

Use the is operator to check if a variable is None in Python, e.g. if my_var is None: . The is operator returns True if the values on the left-hand and right-hand sides point to the same object and should be used when checking for singletons like None .


2 Answers

You can try:

>>> variable = None
>>> isinstance(variable,type(None))
True
>>> variable = True
>>> isinstance(variable,type(None))
False

isinstance takes 2 arguments isinstance(object, classinfo) Here, by passing None you are setting classinfo to None, hence the error. You need pass in the type.

like image 54
karthikr Avatar answered Oct 13 '22 04:10

karthikr


None is not a type, it is the singleton instance itself - and the second argument of isinstance must be a type, class or tuple of them. Hence, you need to use NoneType from types.

from types import NoneType
print isinstance(None, NoneType)
print isinstance(None, (NoneType, str, float))
True
True

Although, I would often be inclined to replace isinstance(x, (NoneType, str, float)) with x is None or isinstance(x, (str, float)).

like image 24
arshajii Avatar answered Oct 13 '22 05:10

arshajii