Understanding Python Objects: Mutable vs Immutable and the id() & type() Functions
This article explores Python’s core concept that everything is an object, explaining how variables reference objects, the role of the id() and type() functions, and the differences between mutable and immutable objects with practical code examples.
One of Python's advantages is its relative ease of use compared to more syntactically heavy languages, but deeper study reveals many hidden features, such as the principle that everything in Python is an object.
Two useful functions: id() and type()
We will explore how Python interacts with mutable and immutable objects using the id() and type() functions. id() takes an object and returns its identity (the memory address in CPython). Comparing IDs of different names shows whether they refer to the same object.
>> a = 5
>>> id(a)
10105216
>>> b = 10
>>> id(b)
10105376The type() function also takes an object but returns its class type instead of its ID.
>> msg = 'hello'
>>> type(msg)
<class 'str'>
>>> age = 10
>>> type(age)
<class 'int'>With this basic understanding, we can start exploring mutable and immutable objects in Python.
Mutable objects
Mutable objects can be changed after creation; they include lists, sets, and dictionaries. Example:
>> list1 = [1, 2, 3]
>>> list2 = list1
>>> id(list1)
140336099032264
>>> id(list2)
140336099032264
>>> list2.append(4)
>>> list1
[1, 2, 3, 4]Both names point to the same object, as shown by the shared ID. Modifying the object through one name affects the other.
If we want a separate copy, we must duplicate the list:
>> list1 = [1, 2, 3]
>>> list3 = list1[:]
>>> id(list1)
140336099032264
>>> id(list3)
140336098233352
>>> list3.append(4)
>>> list1
[1, 2, 3]Creating two independent lists with the same elements also yields different IDs:
>> list1 = [1, 2, 3]
>>> list2 = [1, 2, 3]
>>> id(list1)
140397858622984
>>> id(list2)
140397851306184Each element (e.g., integers) is immutable, but the list container itself is mutable.
Immutable objects
Immutable objects cannot be altered after creation; they include strings, integers, floats, and tuples. Example with strings:
>> string1 = "hello"
>>> string2 = "hello"
>>> id(string1)
140336098225712
>>> id(string2)
140336098225712Both variables share the same ID because strings are immutable. The same holds for other immutable types:
>> a = 5
>>> b = 5
>>> id(a)
10105216
>>> id(b)
10105216Reassigning a variable to a new immutable object creates a new ID:
>> a = 4
>>> id(a)
10105184Mutable and immutable objects in functions
Changes to mutable objects inside a function persist outside the function, while changes to immutable objects do not.
>> def strFunc(oldString):
... oldString = "goodbye"
>>> oldString = "hello"
>>> strFunc(oldString)
>>> print(oldString)
helloThe string remains unchanged because it is immutable and the function does not return a new value.
>> def listFunc(oldList):
... oldList[0] = "goodbye"
>>> oldList = ["hello"]
>>> listFunc(oldList)
>>> print(oldList[0])
goodbyeHere the list is mutable, so the modification is visible after the function call.
Why understanding this matters
Knowing whether you are dealing with mutable or immutable objects can have a significant impact on code behavior, helping you avoid bugs and simplify debugging.
Python treats mutable and immutable objects differently; understanding the type of object you are working with makes it easier to write correct and efficient code.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
