identity

Why does '() is ()' return True when '[] is []' and '{} is {}' return False?

Why does '() is ()' return True when '[] is []' and '{} is {}' return False? Question: From what I’ve been aware of, using [], {} or () to instantiate objects returns a new instance of list, dict or tuple respectively; a new instance object with a new identity. This was pretty clear to …

Total answers: 1

Two variables in Python have same id, but not lists or tuples

Two variables in Python have same id, but not lists or tuples Question: Two variables in Python have the same id: a = 10 b = 10 a is b >>> True If I take two lists: a = [1, 2, 3] b = [1, 2, 3] a is b >>> False according to this …

Total answers: 5

Why is range(0) == range(2, 2, 2) True in Python 3?

Why is range(0) == range(2, 2, 2) True in Python 3? Question: Why do ranges which are initialized with different values compare equal to one another in Python 3? When I execute the following commands in my interpreter: >>> r1 = range(0) >>> r2 = range(2, 2, 2) >>> r1 == r2 True The result …

Total answers: 5

The `is` operator behaves unexpectedly with non-cached integers

The `is` operator behaves unexpectedly with non-cached integers Question: When playing around with the Python interpreter, I stumbled upon this conflicting case regarding the is operator: If the evaluation takes place in the function it returns True, if it is done outside it returns False. >>> def func(): … a = 1000 … b = …

Total answers: 2

Why don't methods have reference equality?

Why don't methods have reference equality? Question: I had a bug where I was relying on methods being equal to each other when using is. It turns out that’s not the case: >>> class What: … def meth(self): … pass >>> What.meth is What.meth # This is False in Python 2 True >>> inst = …

Total answers: 2

Set "in" operator: uses equality or identity?

Set "in" operator: uses equality or identity? Question: class A(object): def __cmp__(self): print ‘__cmp__’ return object.__cmp__(self) def __eq__(self, rhs): print ‘__eq__’ return True a1 = A() a2 = A() print a1 in set([a1]) print a1 in set([a2]) Why does first line prints True, but second prints False? And neither enters operator eq? I am using …

Total answers: 5

Why is a method not identical to itself?

Why is a method not identical to itself? Question: The Python documentation about the is operator says: The operators is and is not test for object identity: x is y is true if and only if x and y are the same object. x is not y yields the inverse truth value. Let’s try that: …

Total answers: 3

Why does id({}) == id({}) and id([]) == id([]) in CPython?

Why does id({}) == id({}) and id([]) == id([]) in CPython? Question: Why does CPython (no clue about other Python implementations) have the following behavior? tuple1 = () tuple2 = () dict1 = {} dict2 = {} list1 = [] list2 = [] # makes sense, tuples are immutable assert(id(tuple1) == id(tuple2)) # also makes …

Total answers: 3

What is the difference between "a is b" and "id(a) == id(b)" in Python?

What is the difference between "a is b" and "id(a) == id(b)" in Python? Question: The id() inbuilt function gives… an integer (or long integer) which is guaranteed to be unique and constant for this object during its lifetime. The is operator, instead, gives… object identity So why is it possible to have two objects …

Total answers: 1