← Back to list

Python3: Mutable, Immutable… everything is object!

Understanding Python Objects: id, type, Mutability, and Function Arguments

Hasanali · 2025-11-18 18:46 · 0 claps · 2.1 min read
#python #oop #mutable-objects #immutable-objects #objects
Open on Medium ↗

Python3: Mutable, Immutable… everything is object!

Understanding Python Objects: id, type, Mutability, and Function Arguments

Python is often called a “high-level language” because it handles many low-level details for you. However, understanding how Python treats objects under the hood is crucial for writing robust and bug-free code. In this post, we’ll explore id and type, mutable vs immutable objects, why it matters, and how Python passes arguments to functions.

1. id and type

In Python, everything is an object, and every object has:

  • An identity: a unique identifier in memory, accessible via id().
  • A type: the class or kind of object it is, accessible via type().
x = 10
print(id(x))    # e.g., 140711682708944
print(type(x))  # <class 'int'>
y = [1, 2, 3]
print(id(y))    # e.g., 140711696367104
print(type(y))  # <class 'list'>
  • id(x) is unique for the object during its lifetime.
  • type(x) tells Python what operations the object supports.

2. Mutable vs Immutable Objects

Objects in Python fall into two categories:

Immutable Objects

  • Cannot be changed after creation.
  • Examples: int, float, str, tuple, frozenset.
  • Any operation that “changes” the object actually creates a new object.
a = 5
print(id(a))  # e.g., 140711682708944
a += 1
print(id(a))  # Different ID! a now points to a new object

Mutable Objects

  • Can be changed in place.
  • Examples: list, dict, set, bytearray.
  • Modifying them does not create a new object.
lst = [1, 2, 3]
print(id(lst))  # e.g., 140711696367104
lst.append(4)
print(id(lst))  # Same ID! Object modified in place

3. Why Mutability Matters

Python’s treatment of mutable and immutable objects affects:

  • Assignment behavior
  • Function argument behavior
  • Memory and performance

For example, with mutable objects:

a = [1, 2, 3]
b = a
b.append(4)
print(a)  # [1, 2, 3, 4] — both names point to the same list

For immutable objects:

x = 10
y = x
y += 1
print(x)  # 10 — x is unaffected, because integers are immutable

Understanding this helps avoid subtle bugs where changing one variable unexpectedly affects another.

4. How Arguments Are Passed to Functions

Python uses “pass-by-object-reference” (sometimes called pass-by-assignment):

  • The function receives a reference to the object, not a copy.
  • Modifying a mutable object inside the function changes it outside.
  • “Modifying” an immutable object creates a new object inside the function, leaving the original unchanged.

Examples:

Immutable Argument

def add_one(n):
    n += 1
    print("Inside function:", n)
x = 10
add_one(x)        # 11
print("Outside:", x)  # 10
  • x remains 10 outside the function because integers are immutable.

Mutable Argument

def append_item(lst):
    lst.append(4)
    print("Inside function:", lst)
my_list = [1, 2, 3]
append_item(my_list)  # [1, 2, 3, 4]
print("Outside:", my_list)  # [1, 2, 3, 4]
  • my_list is changed outside because lists are mutable and the function modified the same object.

5. Summary

  1. **id() gives an object’s identity; `type()`** gives its type.
  2. Immutable objects cannot be changed; operations create new objects.
  3. Mutable objects can be modified in place.
  4. Assignment copies references, not objects.
  5. Function arguments are passed by object reference:
  • Mutable objects → changes affect the caller.
  • Immutable objects → changes do not affect the caller.

Understanding these concepts is key to writing Python code that behaves predictably and efficiently. Whether you’re dealing with lists, strings, or custom objects, knowing how Python treats mutability and references will help you avoid subtle bugs and improve your code quality.


메타데이터
post_id
f40cedcacdb1
slug
python3-mutable-immutable-everything-is-object-f40cedcacdb1
url
https://medium.com/@heseneli536/python3-mutable-immutable-everything-is-object-f40cedcacdb1
canonical_url
https://medium.com/@heseneli536/python3-mutable-immutable-everything-is-object-f40cedcacdb1
author_url
https://medium.com/@heseneli536
status
ok
fetched_at
2026-07-13 06:23:13