python - 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
?
解决方案
推荐阅读
- flutter - 如何从资产中预加载图像?
- wpf - 在 Service 和 ViewModel 之间传递数据
- python - 如何组合不同通道的图像?
- clojure - Clojure 的 SSE(服务器发送事件)客户端库?
- javascript - 设置路由的基本路径
- javascript - 点击 Javascript 将类应用到不同的类
- php - 在 Guzzle 中使用 `sink` 选项下载文件会导致空文件。为什么?以及如何解决?
- string - NASM 字符串切割或操作
- c# - SqlException 而 Linq to Sql
- influxdb - 如何返回 N 个最“重”的请求