首页 > 解决方案 > Reused parameter value in Python between different calls

问题描述

I would presume that the following code:

def f(k, v, d={}):
    if k not in d:
        d[k] = v

    return d[k]


print(f(0, 'a'))
print(f(0, 'b'))

produces the following output:

>>> a
>>> b

but it actually produces:

>>> a
>>> a

What would explain the dictionary d being already assigned on the second call to f?

标签: pythonpython-3.x

解决方案


推荐阅读