Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two variables with the same list have different IDs.....why is that?

Tags:

python

list

Trying to understand the following

Why is it that the ID's assigned by Python are different for the same lists?

x = [1, 2, 3]
y = [1, 2, 3]

id(x) != id(y)
True

id(x)
11428848

id(y)
12943768
like image 980
SQL_rookie Avatar asked Dec 13 '14 15:12

SQL_rookie


People also ask

Can two variables have the same ID in Python?

Object Identity In Python, every object that is created is given a number that uniquely identifies it. It is guaranteed that no two objects will have the same identifier during any period in which their lifetimes overlap.

Why the ID of float values are different when the same value is assigned to two different variables?

In that case the float value containing variable may contain same data but reason behind is to mathematical rules. The only two value are consider after the point & remaining are value get neglected. so the we get two different addresses of the same data containing variables.

Can two objects have the same ID?

Two objects with non-overlapping lifetimes may have the same id() value.


3 Answers

Every distinct object in Python has its own ID. It's not related to the contents -- it's related to the location where the information that describes the object is stored. Any distinct object stored in a distinct place will have a distinct id. (It's sometimes, but not always, the memory address of the object.)

This is especially important to understand for mutable objects -- that is, objects that can be changed, like lists. If an object can be changed, then you can create two different objects with the same contents. They will have different IDs, and if you change one later, the second will not change.

For immutable objects like integers and strings, this is less important, because the contents can never change. Even if two immutable objects have different IDs, they are essentially identical if they have identical contents.

This set of ideas goes pretty deep. You can think of a variable name as a tag assigned to an ID number, which in turn uniquely identifies an object. Multiple variable names can be used to tag the same object. Observe:

>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> id(a)
4532949432
>>> id(b)
4533024888

That, you've already discovered. Now let's create a new variable name:

>>> c = b
>>> id(c)
4533024888

No new object has been created. The object tagged with b is now tagged with c as well. What happens when we change a?

>>> a[1] = 1000
>>> a
[1, 1000, 3]
>>> b
[1, 2, 3]

a and b are different, as we know because they have different IDs. So a change to one doesn't affect the other. But b and c are the same object -- remember? So...

>>> b[1] = 2000
>>> b
[1, 2000, 3]
>>> c
[1, 2000, 3]

Now, if I assign a new value to b, it doesn't change anything about the objects themselves -- just the way they're tagged:

>>> b = a
>>> a
[1, 1000, 3]
>>> b
[1, 1000, 3]
>>> c
[1, 2000, 3]
like image 134
senderle Avatar answered Oct 24 '22 07:10

senderle


The why to that is that if you do that:

l = [1, 2, 3]
m = [1, 2, 3]
l.append(4)

Ids should not be the same and ids must not change for any objects since they identify them. All mutable objects works this way. But it is also the case for tuples (which are unmutable).

Edit: As commented below, the ids may refer to memory address in some python implementation but not in all.

like image 3
Benoît Latinier Avatar answered Oct 24 '22 06:10

Benoît Latinier


Those aren't the same lists. They may contain identical information, but they are not the same. If you made y = x, you'd find that actually the id is the same.

like image 2
Benjamin James Drury Avatar answered Oct 24 '22 05:10

Benjamin James Drury