Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Migrating from Python 2.7 to 3.7 – difference between isinstance(obj, None) vs is None

I have to migrate a project from Python 2.7 to 3.7.

This line of code used to work in 2.7

if isinstance(obj, None):

for some reason it doesn’t anymore. If I modify it this way:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

if isinstance(obj, type(None)):

it will work though.

But my question is, what is the difference between this call:

isinstance(obj, None)

and

obj is None

why did the original devs decided to use isinstance over is? (in py 2.7)

Thanks.

>Solution :

is operator is used to check if both the objects are one and the same, whereas isinstance is used to check if the second parameter appears anywhere in the inheritance chain of the first parameter.

obj is None

obj is actually None.

isinstance(obj, type(None))

Check obj is None type object.

But actually there is only one object of None type. None is the only object of the None Type. So when you specify None type, you write it as type(None).

After all, isinstance(None, type(None)) is true.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading