Concepts / The id() Function and Object Identity

The id() Function and Object Identity

The practical value of object identity is diagnostic. If an unexpected change appears through more than one variable, ask whether those variables reference the same object. The id() function provides a way to inspect that identity relationship.

  • Programming
Interactive lab

Try it: Names and Objects

How Python variables are names bound to objects: assignment never copies, mutating a list is seen through every name that points at it, and ints are replaced rather than changed.

How it works

  1. name = object binds a name; b = a makes b point at the same object.
  2. Mutating a list (append, +=) changes the one shared object.
  3. a = a + [x] and y = y + 1 create NEW objects and re-point one name.
  4. Passing a list to a function passes the reference, so the function can change it.

Default run (7 steps): Two names, one list. Every name is a label that points at an object. … Printed: [1, 2, 3] / True

Simplified: Six fixed scripts on a tiny heap; you choose the values. Object numbers are illustrative, not real id() values.

Educational simulation

Loading the simulation…

A List with Two Names

Suppose two variable names are connected to one list. The important question is not only what values the list contains. You must also ask whether both names refer to the same list object. Variables are references to objects in memory, not the objects themselves. This distinction explains why a change made through one variable can appear when the list is viewed through another variable.

shoplistvariablelist objectshared objectmylistvariablerefers torefers to
What does each variable point to, and do two variable names refer to the same object or to separate objects?

The diagram represents two references leading to one list object. The variable names are not two separate lists merely because there are two names. If both names point to the same object, changing that object through either name affects what the other name observes.

Reading Object Identity

Definition

Object identity is the question of whether two variable references lead to the same object. The id() function is the tool named in this topic for examining that identity. When you reason about two variables, use id() together with the reference model: equal-looking contents do not by themselves tell you whether the variables refer to one shared object or to separate objects.

first variablereferencesame objectidentity agreesfirst variablereferencedifferent objectsidentity differssecond variablereferencesecond variablereferencepoints topoints topoints todoes not share object
How can id() help distinguish one shared object from two separate objects that contain equal values?
Key point

The practical value of object identity is diagnostic. If an unexpected change appears through more than one variable, ask whether those variables reference the same object. The id() function provides a way to inspect that identity relationship.

Tracing a Shared Change

Worked example

Two Variables, One List

A list is associated with two variable names. Predict what happens when the list is changed through one of those names.

  1. 1
    Start with the references

    Treat each variable name as a reference to an object. If both names point to the same list object, there is only one list being referred to.

  2. 2
    Change the list through one name

    The change is made to the shared list object, not to a private list belonging only to the variable used to make the change.

  3. 3
    Inspect the other name

    Because the other variable still refers to that same object, the change is visible through that variable as well.

Result

A change made through one variable affects every variable that points to the same list object.

shoplistlist contents before changelist objectoriginal contentsshoplistchanged contents visiblelist objectupdated contentsmylistsame list objectmylistsame list objectrefers torefers torefers torefers to
What happens to every variable pointing to a list when that list is changed through one variable?

Making an Independent Copy

When you need a second list that can be changed independently, use full slice notation. The source pack gives the form mylist = shoplist[:]. This creates an independent copy rather than another reference to the same list object. The copied list begins with the contents of the original, but later changes to one list do not represent changes made to the other shared object.

shoplistoriginal referenceoriginal listoriginal contentsmylistcopy referencecopied listsame starting contentsrefers torefers to
How does full slicing create a new list object while preserving the original list's contents?
Worked example

Choosing Between Sharing and Copying

You want a second variable to begin with the contents of an existing list, but you do not want later list changes to be shared.

  1. 1
    Identify the risk

    A second variable that points to the same list object will share changes with the first variable.

  2. 2
    Use full slice notation

    Write mylist = shoplist[:]. The full slice notation is the source pack's stated way to create an independent copy.

  3. 3
    Reason about later changes

    The two variable names now refer to separate list objects. Their starting contents come from the original list, but the objects are independent.

Result

Use full slice notation when you need an independent list copy instead of another reference to the original list.

Mistakes with Shared References

  • Common mistake

    Assuming that two variable names automatically mean two list objects.

    What it looks like

    Treating a second name as a private list without checking whether it refers to the original object.

    Why it fails

    Multiple variables can point to the same object.

    Do this instead

    Trace where each variable points. Use object identity reasoning and the id() function when you need to inspect that relationship.

  • Common mistake

    Expecting a change through one variable to remain visible only through that variable.

    What it looks like

    Changing a shared list and then being surprised when another variable shows the change.

    Why it fails

    A change through one variable affects all variables pointing to the same object.

    Do this instead

    Before changing a list, decide whether shared behavior is intended. If not, create an independent copy with full slice notation.

  • Common mistake

    Ignoring object identity because two lists have the same contents.

    What it looks like

    Treating equal-looking contents as proof that two variables refer to one object.

    Why it fails

    Contents and identity answer different questions.

    Do this instead

    Use id() as an identity check and reason separately about the values contained in each list.

Check Your Prediction

MEDIUM

Imagine that two variables point to the same list object. A change is made through the first variable. Before checking the second variable, predict whether it shows the original list contents or the changed contents. Then explain how full slice notation would change the result.

Hints
  • Start by drawing one list object and two arrows or references leading to it.
  • Ask whether the change is made to a variable name or to the shared list object.
  • For independence, use the source form mylist = shoplist[:].

What do you think happens?

If two variables point to the same list object, what happens when the list is changed through one variable?

  • Only the variable used for the change reflects it

  • Every variable pointing to that object reflects it

  • A new independent list is created automatically

Reveal answer
Answer:

Every variable pointing to that object reflects it

The variables are references to the same object. The change affects that object, so it is visible through every reference to it.

The Reference Model

  1. 1
    Variables are references to objects in memory, not the objects themselves.
  2. 2
    Multiple variables can point to one list object, so a change through one variable affects the others.
  3. 3
    The id() function helps you reason about and inspect object identity.
  4. 4
    Equal contents do not by themselves establish that two variables refer to the same object.
  5. 5
    Use full slice notation, such as mylist = shoplist[:], to create an independent list copy.

Key Takeaways

  • A variable refers to an object; it is not the object itself.
  • Several variables may refer to one list, and changes to that shared list are visible through every reference.
  • Object identity is separate from list contents, and id() can be used when investigating that identity.
  • Full slice notation creates an independent list copy so later changes are not shared through the same list object.

Related concepts

More concepts starting with T