可变参数作为对象的默认值的时候
创建不同的对象(均为默认值)的时候,默认值会具有相同的引用
def f(a = []):
a.append(1)
return a
print(f())
print(f())
print(f())
# 输出
# [1]
# [1, 1]
# [1, 1, 1]
正确的写法应将默认值设为None,检查参数后再进行赋值
def f(a = None):
if a is None:
a = []
else:
a = list(a)
a.append(1)
return a
标签:None,python,参数,可变,print,默认值,append
From: https://www.cnblogs.com/meizhengchao/p/16869346.html