Modifying global variable with same name as local variable

Use built-in function globals().

globals()

Return a dictionary representing the current global symbol table. This is always the dictionary of the current module (inside a function or method, this is the module where it is defined, not the module from which it is called).

a = 'foo'

def my_func(a = 'bar'):
    globals()['a'] = a

BTW, it's worth mentioning that a global is only "global" within the scope of a module.


Don't muddle global and local namespaces to begin with. Always try and use a local variable versus a global one when possible. If you must share variables between scopes you can still pass the variables without need for a global placeholder. Local variables are also referenced much more efficiently accessed than globals.

A few links:

Sharing Variables in Python

Variable performance