Consider the function below: def func(number, result=[]): result.append(number) return result We have a classic Python problem here. The default value for the result argument is a list (a mutable object). In Python, the default value of a function argument is only evaluated once. The function was called twice: func(10) func(20) Considering the above, what will be the result of the next call func(30)?

Questions & AnswersCategory: PythonConsider the function below: def func(number, result=[]): result.append(number) return result We have a classic Python problem here. The default value for the result argument is a list (a mutable object). In Python, the default value of a function argument is only evaluated once. The function was called twice: func(10) func(20) Considering the above, what will be the result of the next call func(30)?
Geek Boy Staff asked 2 years ago

Consider the function below:

def func(number, result=[]):
    result.append(number)
    return result

We have a classic Python problem here. The default value for the result argument is a list (a mutable object). In Python, the default value of a function argument is only evaluated once.
The function was called twice:

func(10)
func(20)

Considering the above, what will be the result of the next call func(30)?
a. [20, 30]
b. [10, 20, 30]
c. [30]
d. []
e. 30

1 Answers
Geek Boy Staff answered 2 years ago

b. [10, 20, 30]
Explanation

Python’s default arguments are evaluated once when the function is defined, not each time the function is called.