how could i distinguish default-parameter method in python

53 Views Asked by At

recently,i am learn python.i met a problem when i readed this book named The_Python_Tutorial.it's about how to distinguish the method with default parameter.i googled and read other books while i try to solve it,but i failed.if you can help me,plz. here is my problem:

  def f(a,L=[]):
        L.append(a)
        return L
    print(f(1))
    print(f(2))
    print(f(3))

it shows

[1]
[1,2]
[1,2,3]

so i changed another way

 def g(a,L=None):
            if L is None:
                L=[]
            L.append(a)
            return L
     print (g(1))
     print (g(2))
     print (g(3))

it just shows

[1]
[2]
[3]

how it happens?too weird.all i want to know is that why diff betwween this two method and sommeone can explain it. thanks.

0

There are 0 best solutions below