Is it possible to modify variable in python that is in outer, but not global, scope?

Python 3.x has the nonlocal keyword. I think this does what you want, but I'm not sure if you are running python 2 or 3.

The nonlocal statement causes the listed identifiers to refer to previously bound variables in the nearest enclosing scope. This is important because the default behavior for binding is to search the local namespace first. The statement allows encapsulated code to rebind variables outside of the local scope besides the global (module) scope.

For python 2, I usually just use a mutable object (like a list, or dict), and mutate the value instead of reassign.

example:

def foo():
    a = []
    def bar():
        a.append(1)
    bar()
    bar()
    print a

foo()

Outputs:

[1, 1]

You can use an empty class to hold a temporary scope. It's like the mutable but a bit prettier.

def outer_fn():
   class FnScope:
     b = 5
     c = 6
   def inner_fn():
      FnScope.b += 1
      FnScope.c += FnScope.b
   inner_fn()
   inner_fn()
   inner_fn()

This yields the following interactive output:

>>> outer_fn()
8 27
>>> fs = FnScope()
NameError: name 'FnScope' is not defined

I'm a little new to Python, but I've read a bit about this. I believe the best you're going to get is similar to the Java work-around, which is to wrap your outer variable in a list.

def A():
   b = [1]
   def B():
      b[0] = 2
   B()
   print(b[0])

# The output is '2'

Edit: I guess this was probably true before Python 3. Looks like nonlocal is your answer.