2.20 函数参数接收及判断(避坑)
2.20.1 默认参数为可变类型
在Python中,我们经常会自定义函数,并且有些函数会带有参数。如果参数输入的是空列表,那么可能会如下进行定义:
def myfun(lst=[]):
lst.append(1)
return lst
这个函数看上去平平无奇,但是其实在使用起来会出现大问题。
我们连续调用3次看下效果:
ic(myfun())
ic(myfun())
ic(myfun())
15:32:46|> myfun(): [1]
15:32:47|> myfun(): [1, 1]
15:32:47|> myfun(): [1, 1, 1]</