python - Why is a function argument default value (an empty list) static? -
this question has answer here:
code:
def func(a=[]): a.append(1) print(a) func() func() func()
output:
[1] [1, 1] [1, 1, 1]
i thought default value, list, re-assigned every time func
called, , answer be:
[1] [1] [1]
you "thought default value, list, re-assigned everytime func called." thought wrong. if learning python should work through the official tutorial @ point. here what says default arguments:
the default value evaluated once. makes difference when default mutable object such list, dictionary, or instances of classes.
read tutorial details.
Comments
Post a Comment