Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

Can you modify a variable in Python?

Mutable and immutable typesSome values in python can be modified, and some cannot. This does not ever mean that we can't change the value of a variable – but if a variable contains a value of an immutable type, we can only assign it a new value. We cannot alter the existing value in any way.

Can a function modify a global variable Python?

Use of “global†keyword to modify global variable inside a function. If your function has a local variable with same name as global variable and you want to modify the global variable inside function then use 'global' keyword before the variable name at start of function i.e.

What happens if you modify a variable outside the function give an example?

1 Answer. When a function depends on variables or functions outside of its definition block, you can never be sure that the function will behave the same every time it's called. In the above example the value of y get changed inside the function definition due to which the result will change each time.

Which keyword allows you to modify a variable which is outside of the current scope?

In Python, global keyword allows you to modify the variable outside of the current scope. It is used to create a global variable and make changes to the variable in a local context.


On Python 3, use the nonlocal keyword:

The nonlocal statement causes the listed identifiers to refer to previously bound variables in the nearest enclosing scope excluding globals. 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.

def foo():
    a = 1
    def bar():
        nonlocal a
        a = 2
    bar()
    print(a)  # Output: 2

On Python 2, use a mutable object (like a list, or dict) and mutate the value instead of reassigning a variable:

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.


No you cannot, at least in this way.

Because the "set operation" will create a new name in the current scope, which covers the outer one.