Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Mutable Default Arguments | Mutable Objects and Function Arguments
Common Python Mistakes and How to Fix Them

bookMutable 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.

1234567
def 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']
copy

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.

question mark

Which of the following is a safe default argument value for a function in Python

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 1

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

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

bookMutable 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.

1234567
def 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']
copy

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.

question mark

Which of the following is a safe default argument value for a function in Python

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 1
some-alt