A colleague is seeing unexpected behavior in a function. The function uses a list as a default argument and modifies it. Every time the function is called without this argument, the list seems to retain its changes. What's the likely cause?

  • Caching of previous function calls
  • Incorrect usage of global variables
  • Python's behavior with mutable default arguments
  • The list is defined as a class attribute
The likely cause is Python's behavior with mutable default arguments. The list retains its changes because it's a mutable object shared among calls.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *