Python: function to alter a global variable that is also parameter
<pre><code>def fxn(L):
    """ 
    """
    global L = 2

L = 1
fxn(L)
print(L)
</code></pre>

<p>I have a function like the one above. Assume I need the function to alter the global variable from within the function so that when I print L after calling fxn(L). I end up with the 2 rather than 1. </p>

<p>Is there any way to do this? I cant use global L in the function because L is also a parameter.</p>

<p>This is a bad idea, but there are ways, for example:</p>

<pre><code>a = 5

def f(a):
    def change_a(value):
        global a
        a = value
    change_a(7)

f(0)

print(a)   # prints 7
</code></pre>

<p>In reality, there is seldom any need for writing to global variables. And then there is little chance that the global has the same name as a variable which just cannot change the name.</p>

<p>If you are in such a situation, ask yourself <em>"am i using <code>global</code> too often?"</em></p>

python function global