From the Summerfield's Programming in Python3:
it says as follows: when default values are given, they are created at the time the def statement is executed, not when the function is called. But my question is for the following example:
def append_if_even(x, lst =None):
lst = [] if lst is None else lst
if x % 2 ==0:
lst.append(x)
return lst
As the first time definition executed, lst is point to None But after the function call append_if_even(2),
Shouldn't lst point to [2], since after lst.append(x) lst not point to None anymore ?
Why the next execution still make lst point to none?
- What really happen inside this function call append_if_even(2)?