Search⌘ K

Beware of Default Mutable Arguments!

Understand how Python handles default mutable arguments in functions and why their values persist between calls. Explore common pitfalls this causes and learn best practices to avoid bugs by using None as a default and checking inputs, improving code reliability.

We'll cover the following...

The lesson title says it all.

Python 3.5
def some_func(default_arg=[]):
default_arg.append("some_string")
return default_arg
print(some_func())
print(some_func())
print(some_func([]))
print(some_func())

Explanation

The default mutable arguments ...

...