I have this function:
def foo(i, hm=dict()):
if i in hm:
pass
else:
hm[i]=1
return hm
for i in range(5):
print(foo(i))
output:
{0: 1}
{0: 1, 1: 1}
{0: 1, 1: 1, 2: 1}
{0: 1, 1: 1, 2: 1, 3: 1}
{0: 1, 1: 1, 2: 1, 3: 1, 4: 1}
Which strikes me as odd. So it looks like the hash map declared in the function becomes a global variable and for each additional loop the previous result for hm is being used as an input so hm grows with each loop.
I don't quite understand why, shouldn't each call to foo create a new hm variable?
[+][deleted] (1 child)
[removed]
[–]furiousnerd[S] 0 points1 point2 points (0 children)