Python 3 changing variable in function from another function
<p>I would like to access the testing variable in main from testadder, such that it will add 1 to testing after testadder has been called in main.</p>

<p>For some reason I can add 1 to a list this way, but not variables. The nonlocal declaration doesn't work since the functions aren't nestled.</p>

<p>Is there a way to work around this?</p>

<pre><code>def testadder(test, testing):
    test.append(1)
    testing += 1

def main():
    test = []
    testing = 1
    testadder(test, testing)
    print(test, testing)

main()
</code></pre>

<p>Lists are mutable, but integers are not. Return the modified variable and reassign it. </p>

<pre><code>def testadder(test, testing):
    test.append(1)
    return testing + 1

def main():
    test = []
    testing = 1
    testing = testadder(test, testing)
    print(test, testing)

main()
</code></pre>

python python-3.x python-nonlocal