Python – How to immutably capture the current value of a closure variable in Python?

How to immutably capture the current value of a closure variable in Python?… here is a solution to the problem.

How to immutably capture the current value of a closure variable in Python?

< partition >

If I do

def f():
    a = 1
    g = lambda: a
    a = 2
    return g
print(f()())

The printed value is 2 because a mutates after g construction.
How do I have g statically capture the value of a so that future modifications are ignored?

Related Problems and Solutions