Here are two pieces of codes which were under the standard of python3.6. And they are the examples in the docs of python3.6(tutorial, page25). The first is:
def f(a, L=[]):
L.append(a)
return L
print(f(1))
print(f(2))
print(f(3))
the result:
[1]
[1, 2]
[1, 2, 3]
the second:
def f(a, L = None):
if L is None:
L = []
L.append(a)
return L
print(f(1))
print(f(2))
print(f(3))
the result:
[1]
[2]
[3]
So, in the second piece of code, i am confused that after print(f(1))
was executed, print(f(2))
would pass a = 2
and L=[1]
to the f()
, but why f()
didn't get the L=[1]
?
If L = None
in the second piece of code defines the L
to None
every time when the f()
was called, but why L = []
in the first piece of code don't define L
to []