[Python] Use an empty class to hold a temporary scope
def function_creator():
class FnScope:
b = 5
c = 6
def inner_fn():
FnScope.b += 1
FnScope.c += FnScope.b
print(FnScope.c)
return inner_fn
>>> f = function_creator()
>>> f()
12
>>> f()
19
>>> f()
27
Reference: Python nested functions variable scoping
留言
張貼留言