Python में Mutable और Immutable: पूरी List Examples के साथ
Lists, dicts और sets mutable हैं; int, float, str, tuple immutable. Memory में इसका मतलब, id() proof और classic function-argument trap देखें.
Definition
Mutable objects can be changed after creation (in the same memory). Immutable objects can never change — any "modification" actually creates a brand-new object.
| Mutable (changeable) | Immutable (locked) |
|---|---|
| list, dict, set, bytearray | int, float, bool, str, tuple, frozenset, bytes |
Proof with id()
# MUTABLE: list changes in the SAME memory
nums = [1, 2, 3]
print(id(nums)) # e.g. 140023...
nums.append(4)
print(id(nums)) # SAME id - object modified in place
# IMMUTABLE: string "change" creates a NEW object
name = "Aman"
print(id(name)) # e.g. 140055...
name = name + " Kumar"
print(id(name)) # DIFFERENT id - new object created
140023011
140023011 <- same
140055123
140055987 <- different
Why strings seem changeable but aren't
s = "hello"
s[0] = "H"
# TypeError: 'str' object does not support item assignment
s = "Hello" # this is NOT modification - it's REBINDING
# the name s to a completely new string object
Assignment like
s = "Hello" never modifies the old object — it just points the name to a new one. This distinction between modifying an object and rebinding a name is the heart of this whole topic.The classic trap: mutable function argument
def add_student(name, students=[]): # DANGER: mutable default!
students.append(name)
return students
print(add_student("Aman"))
print(add_student("Priya")) # expected ['Priya'] ...but!
['Aman']
['Aman', 'Priya'] <- Priya joined Aman's list!
The default list is created once at function definition and shared across all calls. Correct pattern:
def add_student(name, students=None):
if students is None:
students = []
students.append(name)
return students
Why immutability matters (interview points)
- Dictionary keys: only immutable (hashable) objects can be dict keys or set members — that's why tuples work but lists don't.
- Safety: immutable objects can be shared between functions/threads with zero risk of accidental modification.
- Aliasing bug:
b = afor a mutable object means both names share one object — change through b appears in a. With immutable objects this bug is impossible.
Definition
Mutable objects बनने के बाद बदले जा सकते हैं (same memory में). Immutable objects कभी नहीं बदलते — कोई भी "modification" असल में बिल्कुल नया object बनाता है.
| Mutable (बदल सकते हैं) | Immutable (locked) |
|---|---|
| list, dict, set, bytearray | int, float, bool, str, tuple, frozenset, bytes |
id() से proof
# MUTABLE: list SAME memory में बदलती है
nums = [1, 2, 3]
print(id(nums)) # जैसे 140023...
nums.append(4)
print(id(nums)) # SAME id - object in place modify हुआ
# IMMUTABLE: string "change" NAYA object बनाता है
name = "Aman"
print(id(name)) # जैसे 140055...
name = name + " Kumar"
print(id(name)) # DIFFERENT id - नया object बना
140023011
140023011 <- same
140055123
140055987 <- अलग
Strings बदलती दिखती हैं पर बदलतीं नहीं
s = "hello"
s[0] = "H"
# TypeError: 'str' object does not support item assignment
s = "Hello" # यह modification NAHI hai - यह REBINDING है
# name s अब बिल्कुल नए string object को point करता है
s = "Hello" जैसा assignment पुराने object को कभी modify नहीं करता — सिर्फ name को नए object पर point करा देता है. Object modify करने और name rebind करने का यही फर्क इस पूरे topic का दिल है.Classic trap: mutable function argument
def add_student(name, students=[]): # खतरा: mutable default!
students.append(name)
return students
print(add_student("Aman"))
print(add_student("Priya")) # expected ['Priya'] ...लेकिन!
['Aman']
['Aman', 'Priya'] <- Priya Aman की list में आ गई!
Default list function definition पर एक बार बनती है और सभी calls में share होती है. सही pattern:
def add_student(name, students=None):
if students is None:
students = []
students.append(name)
return students
Immutability क्यों matter करती है (interview points)
- Dictionary keys: सिर्फ immutable (hashable) objects dict keys या set members बन सकते हैं — इसीलिए tuples चलते हैं, lists नहीं.
- Safety: immutable objects functions/threads के बीच बिना accidental modification के risk के share हो सकते हैं.
- Aliasing bug: mutable object के लिए
b = aका मतलब दोनों names एक object share करते हैं — b से change a में दिखता है. Immutable objects के साथ यह bug impossible है.
Frequently Asked Questions
Python में कौन-से data types mutable और immutable हैं?
Mutable: list, dict, set, bytearray. Immutable: int, float, bool, str, tuple, frozenset, bytes.
क्या Python में string mutable है?
नहीं. Strings immutable हैं — s[0] = "H" TypeError देता है, और s + "x" जैसे operations बिल्कुल नया string object बनाते हैं.
Mutable default argument खतरनाक क्यों है?
क्योंकि default object function definition पर एक बार बनता है और सभी calls में share होता है, जिससे data calls के बीच leak होता है. Default में None रखें और list अंदर बनाएं.