TL;DR

Using mutable objects like empty lists as default arguments in Python can cause persistent state across multiple calls. Use immutable values like None instead.

What happened

Timothy encountered a Python function bug where student enrollments were persisting due to the use of an empty list as a default argument, leading to shared and mutable data issues.

Why it matters for ops

Understanding how default arguments are handled in Python is crucial for maintaining predictable behavior in software operations. Using immutable defaults prevents shared state across function calls.

Action items

  • Review all Python functions using mutable objects as default arguments.
  • Replace mutable defaults with None and initialize within the function body.
  • Use linting tools to detect improper usage of default arguments.

Source link

https://dev.to/aaron_rose_0787cc8b4775a0/the-secret-life-of-python-the-default-trap-2jl0