Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele 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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 1

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Awesome!

Completion rate improved to 5.26

bookMutable Default Arguments

Pyyhkäise näyttääksesi valikon

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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 1
some-alt