a = 10
f = curve_pre()
# note global a will not affect the result, as f will take regional a = 25
print(f(2))

100

Closure = function + environmental variables, so closure will not be affected by the external varialbes.

Closure’s definition has something to do with the environmental variables. If there’s no local variable, then closure will be none; however if there is local varialbe, closure will be defined.

# In this case, because a was not defined within the function, so it will use global variable a
a = 25
# define a curve function
def curve_pre():
def curve(x):
return a * x ** 2
return curve
a = 10
f = curve_pre()
# note global a will not affect the result, as f will take regional a = 25
print(f(2))
print(f.__closure__)