1. 程式人生 > >不可不知:函式中預設引數的陷阱

不可不知:函式中預設引數的陷阱

現象

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