global keyword is used by the python compiler to designate a function name as global.
Once you use it anywhere in a function, that name is no longer a local name.
Please note that if does not introduce a new scope, only functions and modules (with classes, lists, dict and established understandings are special cases of function areas).
A (hard to read and non-python) job will be to use the globals() function:
def foo(a): if a: globals()['g'] = 10 else: g = 20
source share