python列表作为默认参数的问题
# coding=utf-8 def foo(a, x=[]): x.append(a) return x print(foo(1)) print(foo(2)) # 预想输出 # [1] # [2] # 实际输出 # [1] # [1, 2]
正确示例:
# 正确示例 def foo1(a, x=None): if x is None: x = [] x.append(a) return x print(foo1(1)) print(foo1(2)) # [1] # [2]