I don't know if there is an attribute of a function that gives __dict__ outer space of a function when that outer space is not a global space == module, which takes place when a function is a nested function, in Python 3.
But in Python 2, as far as I know, there is no such attribute.
Thus, the only possibilities to do what you want:
1) using a mutable object, as others say
2)
def A() : b = 1 print 'b before B() ==', b def B() : b = 10 print 'b ==', b return b b = B() print 'b after B() ==', b A()
result
b before B() == 1 b == 10 b after B() == 10
,
Nota
Cédric Julien's solution has the disadvantage of:
def A() : global b # N1 b = 1 print ' b in function B before executing C() :', b def B() : global b # N2 print ' b in function B before assigning b = 2 :', b b = 2 print ' b in function B after assigning b = 2 :', b B() print ' b in function A , after execution of B()', b b = 450 print 'global b , before execution of A() :', b A() print 'global b , after execution of A() :', b
result
global b , before execution of A() : 450 b in function B before executing B() : 1 b in function B before assigning b = 2 : 1 b in function B after assigning b = 2 : 2 b in function A , after execution of B() 2 global b , after execution of A() : 2
Global b after executing A() been changed and cannot be
This is the case only if there is an object with identifier b in the global namespace