我有一个带有一个默认参数的函数。此默认参数在函数内更新。 当我再次调用相同的函数时,我看到参数采用在第一次调用期间设置的值
以下代码有什么问题。
def fun(x, data=[]):
print "entering fun with data = ",data
data.append(x)
print "calling fun..."
fun(1)
print "calling fun..."
fun(3)
这里的输出是
calling fun...
entering fun with data = []
calling fun...
entering fun with data = [1]
但我希望输出为
enter code here
calling fun...
entering fun with data = []
calling fun...
entering fun with data = []