empty dictionary as default value for keyword argument in python function: dictionary seems to not be initialised to {} on subsequent calls? [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-12-03 01:14:15
>>> def f(i, d=None, x=3):
...     if not d:
...         d={}
...     d[i] = i*i
...     x += i
...     return x,d
... 
>>> f(2)
(5, {2: 4})
>>> f(3)
(6, {3: 9})
>>> 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!