employer cover photo
employer logo
employer logo

Simons Foundation

Is this your company?

Simons Foundation Interview Question

In Python, in an "if" statement, how would you check if something is None? What is the difference between "if foo is None" and "if foo == None" and why would you prefer one over the other?

Interview Answer

Anonymous

Nov 9, 2016

"if foo is None" is preferred. It checks for identity. In contrast, "if foo == None" checks for equality. There is only one None object instance in CPython. PEP8 recommendations aside, checking for equality is slower than checking for identity. Also in Python, the equality comparison method __eq__ can be overridden.