• Home
  • 2.11 Identity and Memory Address

2.11 Identity and Memory Address

View Categories

2.11 Identity and Memory Address

3 min read

Every value in Python is stored as an object in memory. Each object has:

  • A value
  • A data type
  • A unique identity

The identity of an object is a unique number that remains constant during the object’s lifetime. In CPython, this identity is usually the memory address of the object.

Python provides the built-in id() function to view the identity of an object.

Understanding object identity helps explain how variables work internally and why assigning one variable to another does not always create a new object.


Syntax #

id(object)

Parameter #

Parameter Description
object The object whose identity is to be obtained.

Return Value #

The id() function returns a unique integer representing the identity of the object.

Note: The exact number returned by id() varies from one program execution to another. Therefore, your output may be different from the examples shown here.


Example 1: Getting the Identity of an Object #

main.py

number = 100

print(id(number))

Sample Output #

140654845202736

Explanation #

  • The variable number refers to an integer object.
  • The id() function returns the identity of that object.
  • The value shown is unique for that object during the program’s execution.
  • The exact number will differ on your computer.

Example 2: Identity of Different Objects #

main.py

a = 100
b = 200

print(id(a))
print(id(b))

Sample Output #

140654845202736
140654845205936

Explanation #

  • a and b refer to different integer objects.
  • Since they are different objects, their identities are different.
  • Each object has its own unique identity.

Example 3: Two Variables Referring to the Same Object #

main.py

name1 = "Python"
name2 = name1

print(id(name1))
print(id(name2))

Sample Output #

140654846012944
140654846012944

Explanation #

  • name1 refers to the string "Python".
  • name2 = name1 does not create another string.
  • Both variables refer to the same object.
  • Therefore, both variables have the same identity.

Example 4: Checking Identity Using is #

main.py

a = "Python"
b = a

print(a is b)

Output #

True

Explanation #

  • The is operator checks whether two variables refer to the same object.
  • Since both variables refer to the same object, the result is True.
  • The is operator compares identities, not values.

Example 5: Equality vs Identity #

main.py

a = [10, 20, 30]
b = [10, 20, 30]

print(a == b)
print(a is b)

Output #

True
False

Explanation #

  • a == b compares the values stored in both lists.
  • Since both lists contain the same elements, the result is True.
  • a is b compares their identities.
  • The two lists are different objects, so the result is False.

Example 6: Assignment Does Not Create a New Object #

main.py

numbers1 = [10, 20, 30]

numbers2 = numbers1

print(numbers1 is numbers2)

Output #

True

Explanation #

  • numbers2 is assigned the reference stored in numbers1.
  • Both variables refer to the same list object.
  • No new list is created during assignment.

Example 7: Creating a New Object #

main.py

numbers1 = [10, 20, 30]

numbers2 = numbers1.copy()

print(numbers1 == numbers2)
print(numbers1 is numbers2)

Output #

True
False

Explanation #

  • copy() creates a new list.
  • Both lists contain the same values.
  • However, they are separate objects in memory.
  • Therefore, is returns False.

Example 8: Identity After Reassignment #

main.py

value = 100

print(id(value))

value = 200

print(id(value))

Sample Output #

140654845202736
140654845205936

Explanation #

  • Initially, value refers to the integer object 100.
  • After reassignment, it refers to another integer object 200.
  • The identity changes because the variable now refers to a different object.

Example 9: Using id() with Different Data Types #

main.py

print(id(10))
print(id(3.14))
print(id("Python"))

Sample Output #

140654845201456
140654846314832
140654846012944

Explanation #

  • Every Python object has an identity.
  • Integers, floating-point numbers, strings, lists, and all other objects have unique identities.
  • The exact values printed will vary between program executions.

Example 10: Variables Are References #

main.py

a = [1, 2, 3]

b = a

b.append(4)

print(a)
print(b)

Output #

[1, 2, 3, 4]
[1, 2, 3, 4]

Explanation #

  • Both a and b refer to the same list object.
  • append(4) modifies that shared list.
  • Since both variables reference the same object, the change is visible through both variables.
  • This demonstrates that variables store references to objects, not the objects themselves.

Common Mistakes #

1. Confusing == with is #

Incorrect

a = [1, 2]

b = [1, 2]

print(a is b)

Incorrect Expectation #

True

Actual Output #

False

Reason

is compares object identities, while == compares values.

Correct

print(a == b)

2. Assuming Assignment Creates a Copy #

Incorrect Assumption

list1 = [10, 20]

list2 = list1

Reason

Both variables refer to the same object.

To create a separate list:

list2 = list1.copy()

3. Relying on Exact id() Values #

Incorrect Assumption

print(id(100))

will always produce the same number.

Reason

The value returned by id() depends on the Python implementation and changes between program executions.


Best Practices #

  • Use == to compare values.
  • Use is only when checking whether two variables refer to the same object.
  • Use id() mainly for learning and debugging.
  • Use .copy() when you need an independent copy of a mutable object such as a list.

Key Points to Remember #

  • Every Python value is an object.
  • Every object has a unique identity.
  • The id() function returns an object’s identity.
  • Variables store references to objects.
  • The is operator compares object identities.
  • The == operator compares object values.
  • Assignment creates another reference to the same object; it does not automatically create a copy.
  • The numeric value returned by id() is implementation-dependent and may differ each time the program runs.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *