I got this question from the answer of this post.
consider this code
def test(a,dict={}):
b=5
dict[a]=1
print dict
print locals()
test(1)
test(2)
The output is:
{1: 1}
{'a': 1, 'b': 5, 'dict': {1: 1}}
{1: 1, 2: 1}
{'a': 2, 'b': 5, 'dict': {1: 1, 2: 1}}
As I can infer, there is a "global" reference to the dict.
what is passed as default parameter to the function is persistent somewhere in the namespace.
It is shared across when the function is called again. but how do I know what the current dict holds. I can have a dict outside of the function and pass that dict to the function to know what the dict holds.
But my question is where the default parameter of dict is present (in the namespace) and how to access it. when is this dict created? when the function is called first time or when the def statement is executed?
btw, printing the locals() shows that dict is local to function
Thanks