fjsj fjsj - 1 year ago 86
Python Question

Python "is" statement: what is happening?

I was quite surprised when

[] is not []


evaluated to
True
.

What is happening in this code? What really
not
and
is
statements are doing?

Answer Source

a is not b is a special operator which is equivalent to not a is b.

The operator a is b returns True if a and b are bound to the same object, otherwise False. When you create two empty lists you get two different objects, so is returns False (and therefore is not returns True).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download