X
X
xxx2018-10-20 16:57:18
Python
xxx, 2018-10-20 16:57:18

What is the scope of l?

There is a code

def f(x,l=[]):
    for i in range(x):
        l.append(i)
    print(l)
f(3)
f(4)

Conclusion
[0, 1, 2]
[0, 1, 2, 0, 1, 2, 3]

I understand that l is not a static variable, because after the end of the function, the reference to the object is not deleted? Then l is enclosing? Does closure work here?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question