On Python 3, use the nonlocal keyword:
The
nonlocalstatement 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]
Answer from Adam Wagner on Stack OverflowOn Python 3, use the nonlocal keyword:
The
nonlocalstatement 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
Why can I access some variables from the outer scope but not others
Accessing the outer scope in Python 2.6 - Stack Overflow
Being able to use a non-local variable inside a local scope without referencing as a parameter?
Is it bad practice to use variables in functions that are not defined in parameters
global only works within the module it's used in. You also don't use global to declare a global variable at the global scope (ie. the module scope). You would use global within the scope of a function to indicate that you want to use a variable in the global scope and not in the local scope.
In python 3, there is also the nonlocal keyword that allows you to indicate you want to modify a variable in an outer scope that isn't the global/module scope.
global
A = 1
def global_func():
global A
A = 2
def func():
A = 3
print(A)
# 1
global_func()
print(A)
# 2
func()
print(A)
# 2
nonlocal
def outside():
a = 1
def inside_nonlocal():
nonlocal a
a = 2
def inside():
a = 3
print(a)
# 1
inside_nonlocal()
print(a)
# 2
inside()
print(a)
# 2
I am not clear what your problem actually is. Is it not possible to do the following?
import backend
if __name__ == "__main__":
# parse arguments
backend.argreceive(arguments)
As Daniel Roseman suggested, if there are many backend functions requiring access to these variables then you should consider using a class, and class properties, to store the variables.
class argreceive():
def __init__(self,args):
self.args = args
Hello.
Consider this code:
def calculate_sum_ways(n):
string = ''
array = []
def sum_ways(n):
if n == 1:
string += '1' # Cant access string from outer scope
array.append('') # But I can access array from outer scope
...Accessing the string gives the error unresolved reference.. But accessing the array here does not. What is going on?
Define the variables outside of the functions and use the global keyword.
s, n = "", 0
def outer():
global n, s
n = 123
s = 'qwerty'
modify()
def modify():
global n, s
s = 'abcd'
n = 456
Sometimes I run across code like this. A nested function modifies a mutable object instead of assigning to a nonlocal:
def outer():
s = [4]
def inner():
s[0] = 5
inner()