How to modify variables of another scope?
问题 Currectly, I'm trying the following: Some mutator function accepts other function's local scope using locals() def mutator(locals): locals['n'] = 10 locals['l'].append(10) def f(): n = 1 l = [1] mutator(locals()) print(n,l) # >>> 1, [1,10] f(None) and that works as expected: I was able to modify mutable list contents but failed to 'rebind' another immutable int to the same name. Currently, I see some options: eval() , exec() & Co Play with stack frames & local variables in mutator() Keep such