Note - in python3 variable shadowing is much more controllable - in your example having modify_n look like:<p><pre><code>    def modify_n(new_value):
       nonlocal n
</code></pre>
(and of course the print x -&gt; print(x) changes), will result in 4,7,7