TGViewer
PyNotes PyNotes @py_notes_uz · 207 subscribers
Post #291 232
Why default argument [] dangerous in Python?

e.g def add_item(item, items=[])


In Python, default arguments are evaluated once at function definition time, not per call.
If the default is a mutable object like a list or dict, it will be shared between function calls, which can cause hidden state and subtle bugs.
The common safe pattern is to use None as the default and create the object inside the function.


def add_item(item, items=[]):
items.append(item)
return items

add_item(1) // returns [1]
add_item(2) // returns [1, 2]


Safe way is:

def add_item(item, items=None):
if items is None:
items = []
items.append(item)
return items
More from @py_notes_uz
  1. Sep 10, 2026👋 Accepted AI Men sizga orzuingizdagi ishga qabul qilinishda yordam beraman: 📊 CV'ingizn…
  2. May 5, 2026Middle/Senior Full Stack (Python+TypeScript) | Sigma Software Qualifications • 6+ years of…
  3. Apr 16, 2026🇺🇿 o'zbek tilida 🇷🇺 на русском AI Incubator 2026 — Applications Open 🚀 Launch your AI…
  4. Mar 24, 2026Want to learn how AI agents are built at Yandex? The Yandex School of Data Analysis is run…
  5. Feb 17, 2026💡 Python bo'yicha ko'nikmalaringizni sanoat standartlari darajasiga oshirishni va mutaxas…
  6. Dec 1, 2025What is Deadlock: A deadlock occurs when two or more tasks are waiting for each other’s re…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →