lst is evaluated when the file is interpreted, hence it's always the same thing in memory. It's one of the gotchas of Python, you can see it here:
lst
>>> def func(a, lst=[]): ... print(id(lst)) ... >>> func(3) 4451985672 >>> func(4) 4451985672
As you can see lst points to the same object in memory.
If you pass an argument it will substitute the one defined at evaluation time:
>>> r = [] >>> def func(a, lst=[]): ... lst.append(a) ... return lst ... >>> func(1, r) [1] >>> func(2, r) [1, 2] >>> r [1, 2] >>> func(4) [4]
Notice how when I don't pass an external list as an argument, it starts using the default one.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
lst
is evaluated when the file is interpreted, hence it's always the same thing in memory. It's one of the gotchas of Python, you can see it here:As you can see
lst
points to the same object in memory.If you pass an argument it will substitute the one defined at evaluation time:
Notice how when I don't pass an external list as an argument, it starts using the default one.