If I run this snippet of code, I expect to see [0] returned twice. Instead, I see [0,1].
def append(flist=[]):
# append the length of a list to the list
flist.append(len(flist))
return flist
print(append())
print(append())
Why does this happen? Shouldn't the expression be reset because of the scope? How do I access flist if it exists outside the function?
This example comes from https://www.toptal.com/python#hiring-guide on question "Q: What will be printed out by the second append() statement below?". It does contain a brief explanation, but I don't understand how this is possible. Anything I can think of to analyze memory hasn't shown me the values, but I'm also not sure what the right tools are to figure this out. Looking at things like locals() or dir() doesn't seem to produce anything meaningful.