Mutable Default Arguments
Understanding how default arguments work in Python functions is crucial, especially when those arguments are mutable types such as lists or dictionaries. In Python, default argument values are evaluated only once at the time the function is defined, not each time the function is called. This means that if you use a mutable object like a list or dict as a default value, all calls to that function that do not provide that argument will share the same object.
1234567def add_item(item, items=[]): items.append(item) return items print(add_item("apple")) # Output: ['apple'] print(add_item("banana")) # Output: ['apple', 'banana'] print(add_item("orange")) # Output: ['apple', 'banana', 'orange']
This behavior can lead to confusing and unintended results. In the code sample above, each call to add_item without specifying the items argument modifies the same list. As a result, items accumulate across calls, which is rarely what you want. This happens because the default list is created once when the function is defined, and the same list is used every time the function is called without a new items argument.
To avoid this pitfall, you should use None as the default value for arguments that are intended to be mutable objects. Inside the function, you can then check if the argument is None and create a new list or dictionary as needed. This ensures that each call to the function gets its own fresh object, preventing unwanted sharing of state between calls.
Danke für Ihr Feedback!
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Can you show me how to rewrite the function using `None` as the default argument?
Why does Python evaluate default arguments only once?
Are there other common pitfalls with default arguments in Python?
Awesome!
Completion rate improved to 5.26
Mutable Default Arguments
Swipe um das Menü anzuzeigen
Understanding how default arguments work in Python functions is crucial, especially when those arguments are mutable types such as lists or dictionaries. In Python, default argument values are evaluated only once at the time the function is defined, not each time the function is called. This means that if you use a mutable object like a list or dict as a default value, all calls to that function that do not provide that argument will share the same object.
1234567def add_item(item, items=[]): items.append(item) return items print(add_item("apple")) # Output: ['apple'] print(add_item("banana")) # Output: ['apple', 'banana'] print(add_item("orange")) # Output: ['apple', 'banana', 'orange']
This behavior can lead to confusing and unintended results. In the code sample above, each call to add_item without specifying the items argument modifies the same list. As a result, items accumulate across calls, which is rarely what you want. This happens because the default list is created once when the function is defined, and the same list is used every time the function is called without a new items argument.
To avoid this pitfall, you should use None as the default value for arguments that are intended to be mutable objects. Inside the function, you can then check if the argument is None and create a new list or dictionary as needed. This ensures that each call to the function gets its own fresh object, preventing unwanted sharing of state between calls.
Danke für Ihr Feedback!