Refer a global variable that has same name as the local variable in Python
<p>I am new to python, how can we refer a global variable that has the same name as local one. </p>

<pre><code>spam = 'global spam'
def scope_test():
    spam = 'local spam'
    print(spam)
    # access global spam and print or assign to the local spam
    # print(global.spam)
    # local.spam = global.spam (something like this)

scope_test()
</code></pre>

<p>It's something not recommended, I am answering it for the sake if you're curious to ask/do it:</p>

<pre><code>Python 3.5.2 
&gt;&gt;&gt; spam = 'global spam'
&gt;&gt;&gt; def scope_test(): 
..     spam = 'local spam' 
..     print(spam) 
..     # access global spam and print or assign to the local spam 
..     print(globals()['spam']) 
..     spam = globals()['spam'] 
..     print(spam) 
..     
&gt;&gt;&gt; scope_test()
</code></pre>

<p>The output:</p>

<pre><code>local spam
global spam
global spam
</code></pre>

python