Ekaterina Kalache
Ekaterina Kalache

Reputation: 91

Object id in Python

Let's assume I execute the following:

>>> id(a)
139926795932424
>>> a
[1, 2, 3, 4]
>>> a = a + [5]
>>> id(a)

Will the last line of this script print 139926795932424? Or a new id will be assigned to a?

Upvotes: 3

Views: 5627

Answers (4)

Austin Theriault
Austin Theriault

Reputation: 91

>>> a= [1,2,3]
>>> a
[1, 2, 3]
>>> id(a)
39384488
>>> a + [5]
[1, 2, 3, 5]
>>> id(a)
39384488

No the id() does not change as you are not creating a new instance or lifetime of the variable. See the documentation for id().

EDIT:

The above example does not reassign the variable like in the question. In that scenario it would be a new variable as a = a + [5] returns a new object.

Upvotes: 1

Chris
Chris

Reputation: 22953

Will the last line of this script print 139926795932424? Or a new id will be assigned to a?

A new id will be assigned to a. Why? Because a refers to a different object.

a = a + [5] is syntactic sugar for a = a.__add__([5]), and since a.__add__([5]) returns a new object, a holds a reference to the new object rather than the old object. This can be observed with a simple example:

>>> a = [1, 2]
>>> b = a # copy the reference in a to b
>>> id(a) == id(b) # they match
True
>>> a = a + [5] # syntatic sugar for a = a.__add__([5])
>>> id(a) == id(b) # they no longer match
False
>>> 

Note that in that sense, a = a + [5] and a += [5] are not identical even though they produce the same result (a = [1, 2, 5]). The first example as said above, is syntactic sugar for a = a.__add__([5]). While the second is syntactic sugar for a = a.__iadd__([5]). The difference is that the latter method - __iadd__() - does not create a new object in memory. Rather, it modifies the existing list object, and returns a reference to it. Again like previously:

>>> a = [1, 2]
>>> b = a # copy the reference in a to b
>>> id(a) == id(b) # they match
True
>>> a += [5]
>>> id(a) == id(b) # they still match, `b` has mutated too!
True
>>> b
[1, 2, 5]

Upvotes: 7

Spencer Cheng
Spencer Cheng

Reputation: 31

In Python, objects are given unique identification numbers that are “guaranteed to be unique and constant for the object during its lifetime.”

In your example, the line a = a + [5] is directly changing the list a and when we call the id() function on the list, we will see a different unique id number. This essentially creates a new list (new object!)

>>> a = [1, 2, 3, 4]
>>> id(a)
140250098481992

>>> a = a + [5]
>>> id(a)
140250098532296

Upvotes: 0

Aurélien Gasser
Aurélien Gasser

Reputation: 3120

From the documentation:

id(object)

Return the “identity” of an object. This is an integer (or long integer) which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.

CPython implementation detail: This is the address of the object in memory.

Since you assigned a new value to a, this new object will have another id.

See more details in this question.

Upvotes: 3

Related Questions