不可不知:函数中默认参数的陷阱

现象

def foo(x, y=[]):
    y.append(x)
    return y
print(foo(1))
print(foo(1, [3, 4]))
print(foo(5))

'''
[1]
[3, 4, 1]
[1, 5]
'''

  


see  also:

  http://www.cnblogs.com/JetpropelledSnake/p/9105286.html

  http://cenalulu.github.io/python/default-mutable-arguments/

  https://pythonguidecn.readthedocs.io/zh/latest/writing/gotchas.html

  

猜你喜欢

转载自www.cnblogs.com/Neeo/p/7709366.html
今日推荐