我是一个有用的助手,可以为您翻译文本。
我正在阅读Python Documentation Release 2.7.10中的Python教程,遇到了这样的内容。
代码
如果第一个函数
我正在阅读Python Documentation Release 2.7.10中的Python教程,遇到了这样的内容。
代码
def fun1(a,L=[]):
L.append(a)
return L
print fun1(1)
print fun1(2)
print fun1(3)
def fun2(a,L = None):
if L is None:
L=[]
L.append(a)
return L
print fun2(1)
print fun2(2)
print fun2(3)
输出
[1]
[1, 2]
[1, 2, 3]
[1]
[2]
[3]
Process finished with exit code 0
如果第一个函数
fun1()
中的L=[]
只被调用一次,那么fun1()
的输出就很好。但是为什么fun2()
中每次都会调用L=None
呢?
L=None
不会在每次调用时被执行?- 我不知道你想表达什么意思。 - decezeL=None
每次都被调用/执行。这就是为什么第二个函数fun2()
中的条件if L is None
每次都为真的原因。 - Nitin Pandey