(初学者求解答)python默认参数
python tutorial 里面有这么一段话:
If you don’t want the default to be shared between subsequent calls, you can write the function like this instead:
def f(a, L=None):
if L is None:
L = []
L.append(a)
return L
这样究竟是怎么做到默认参数不被后续调用共享呢?
另外,按下面这么编写也是能做到默认参数不被后续共享。求原因
Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> def f(a,L=[]):
... if L==[]:
... L=[]
... L.append(a)
... return L
...
>>> f(1)
[1]
>>> f(2)
[2]
>>> f(3)
[3]
>>>