Pergunta de entrevista da empresa Simons Foundation

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?

Resposta da entrevista

Sigiloso

9 de nov. de 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.