Friday, 30 January 2026

Day 43: Mutating Arguments Passed to Functions

 

๐Ÿ Python Mistakes Everyone Makes ❌

Day 43: Mutating Arguments Passed to Functions

This is one of those bugs that looks harmless, works fine in small tests —
and then causes mysterious behavior later in production.


❌ The Mistake

Modifying a mutable argument (like a list or dictionary) inside a function.

def add_item(items):
    items.append("apple") # ❌ mutates the caller's list

my_list = []
add_item(my_list)
print(my_list) # ['apple']

At first glance, this seems fine.
But the function silently changes data it does not own.


❌ Why This Is Dangerous

  • ❌ Side effects are hidden

  • ❌ Makes debugging extremely hard

  • ❌ Breaks assumptions about data immutability

  • ❌ Functions stop being predictable

  • ❌ Reusing the function becomes risky

The caller didn’t explicitly ask for the list to be modified — but it happened anyway.


⚠️ A More Subtle Example

def process(data):
    data["count"] += 1 # ❌ mutates shared state

If data is shared across multiple parts of your app, this change ripples everywhere.


✅ The Correct Way: Avoid Mutation

✔️ Option 1: Work on a copy

def add_item(items):
    new_items = items.copy()
    new_items.append("apple")
    return new_items

my_list = [] 
my_list = add_item(my_list)

Now the function is pure and predictable.


✔️ Option 2: Be explicit about mutation

If mutation is intentional, make it obvious:

def add_item_in_place(items): 
items.append("apple")

Clear naming prevents surprises.


๐Ÿง  Why This Matters

Functions should:

  • Do one thing

  • Have clear contracts

  • Avoid unexpected side effects

Predictable code is maintainable code.


๐Ÿง  Simple Rule to Remember

๐Ÿง  If a function mutates its arguments, make it explicit or avoid it.

When in doubt:

Return new data instead of modifying input.


๐Ÿš€ Final Takeaway

Hidden mutation is one of Python’s most common foot-guns.

Write functions that:

  • Are safe to reuse

  • Don’t surprise callers

  • Make data flow obvious

Your future self (and teammates) will thank you.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (118) AI (190) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (28) Azure (8) BI (10) Books (261) Bootcamp (1) C (78) C# (12) C++ (83) Course (84) Coursera (299) Cybersecurity (29) data (1) Data Analysis (25) Data Analytics (18) data management (15) Data Science (252) Data Strucures (15) Deep Learning (106) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (18) Finance (9) flask (3) flutter (1) FPL (17) Generative AI (54) Git (9) Google (47) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (229) Meta (24) MICHIGAN (5) microsoft (9) Nvidia (8) Pandas (13) PHP (20) Projects (32) Python (1245) Python Coding Challenge (992) Python Mistakes (43) Python Quiz (406) Python Tips (5) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (46) Udemy (17) UX Research (1) web application (11) Web development (8) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)