Отличия между is None и == None
В чём отличие между is None и == None? Почему вторым способом нельзя пользоваться?
Ответы (1 шт):
Автор решения: hedgehogues
→ Ссылка
Для встроенных типов всё будет работать ожидаемо. Но для пользовательских объектов -- нет. Например:
class MyClass:
def __eq__(self, my_object):
# Просто вернем True
return True
my_class = MyClass()
if my_class is None:
print('my_class is None, using the is keyword')
else:
print('my_class is not None, using the is keyword')
if my_class == None:
print('my_class is None, using the == syntax')
else:
print('my_class is not None, using the == syntax')
Вывод:
my_class is not None, using the is keyword
my_class is None, using the == syntax