What is the scope of a defaulted parameter in Python?

前端 未结 7 1524
攒了一身酷
攒了一身酷 2020-12-13 04:34

When you define a function in Python with an array parameter, what is the scope of that parameter?

This example is taken from the Python tutorial:

de         


        
相关标签:
7条回答
  • 2020-12-13 05:15

    There's even less "magic" than you might suspect. This is equivalent to

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

    m is only created once.

    0 讨论(0)
提交回复
热议问题