Python default scope

I am learning Python and came across an example that I don't quite understand. In the official textbook, the following code is given:

i = 5

def f(arg=i):
    print(arg)

i = 6
f()

Based on C ++, it makes sense for me to intuitively understand that this will print 5. But I would also like to understand the technical explanation: "The default values ​​are evaluated at the function definition point in the defining area." What does "defining area" mean here?

+4
source share
1 answer
1. i = 5
2. 
3. def f(arg=i):
4.     print(arg)
5. 
6. i = 6
7. f()

In # 1, it evaluates i = 5, and the variable and its value are added to the scope.

3 . . i 5, arg 5 ( i).

i 6, arg 5, .

+1

All Articles