<div>I have an update:</div><div><br></div><div>I can easily undertand why this example doesn&#39;t work:</div><div><br></div><div>def nochange(x):</div><div>    x = 0</div><div><br></div><div>y = 1</div><div>nochange(y)</div>
<div>print y # Prints out 1</div><div><br></div><div>X is a local variable, and only gets modified in the function, that doesn&#39;t return any value.</div><div><br></div><div>But it&#39;s very difficult for me to understand WHY this works:</div>
<div><br></div><div>def change(some_list):</div><div>    some_list[1] = 4</div><div><br></div><div>x = [1,2,3]</div><div>change(x)</div><div>print x # Prints out [1,4,3]</div><div><br></div><div>some_list is a &quot;local&quot; list, isn&#39;t it? Maybe i can&#39;t have lists that are only existing in a function?</div>
<div><br></div><div>Thankyou all</div><div><br><div class="gmail_quote">2010/2/22 Kent Johnson <span dir="ltr">&lt;<a href="mailto:kent37@tds.net">kent37@tds.net</a>&gt;</span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5">On Mon, Feb 22, 2010 at 9:13 AM, Giorgio &lt;<a href="mailto:anothernetfellow@gmail.com">anothernetfellow@gmail.com</a>&gt; wrote:<br>
<br>
&gt; And, i have some difficulties understanding the other &quot;strange&quot; example in<br>
&gt; that howto. Just scroll down to: &quot;However, the point is that the value<br>
&gt; of x is picked up from the environment at the time when the function is<br>
&gt; defined. How is this useful? Let’s take an example — a function which<br>
&gt; composes two other functions.&quot;<br>
&gt; He is working on a function that compose other 2 functions. This is the<br>
&gt; final solution<br>
&gt; def compose(fun1, fun2):<br>
&gt;     def inner(x, fun1=fun1, fun2=fun2):<br>
&gt;         return fun1(fun2(x))<br>
&gt;     return inner<br>
&gt; But also tries to explain why this example:<br>
&gt; # Wrong version<br>
&gt; def compose(fun1, fun2):<br>
&gt;     def inner(x):<br>
&gt;         return fun1(fun2(x))<br>
&gt;     return inner<br>
&gt; def fun1(x):<br>
&gt;     return x + &quot; world!&quot;<br>
&gt; def fun2(x):<br>
&gt;     return &quot;Hello,&quot;<br>
&gt; sincos = compose(sin,cos)  # Using the wrong version<br>
&gt; x = sincos(3)<br>
&gt; Won&#39;t work. Now, the problem is that the &quot;inner&quot; function gets fun1 and fun2<br>
&gt; from other 2 functions.<br>
&gt; My question is: why? inner is a sub-function of compose, where fun1 and fun2<br>
&gt; are defined.<br>
<br>
</div></div>It does work:<br>
In [6]: def compose(fun1, fun2):<br>
<div class="im">   ...:     def inner(x):<br>
   ...:         return fun1(fun2(x))<br>
   ...:     return inner<br>
</div>   ...:<br>
<br>
In [7]: def fun1(x):<br>
   ...:         return x + &quot; world!&quot;<br>
   ...:<br>
<br>
In [8]: def fun2(x):<br>
   ...:         return &quot;Hello,&quot;<br>
   ...:<br>
<br>
In [9]: from math import sin, cos<br>
<br>
In [10]: sincos = compose(sin,cos)  # Using the wrong version<br>
<br>
In [11]:<br>
<br>
In [12]: x = sincos(3)<br>
<br>
In [13]:<br>
<br>
In [14]: x<br>
Out[14]: -0.8360218615377305<br>
<br>
That is a very old example, from python 2.1 or before where nested<br>
scopes were not supported. See the note &quot;A Note About Python 2.1 and<br>
Nested Scopes&quot; - that is now the default behaviour.<br>
<font color="#888888"><br>
Kent<br>
</font></blockquote></div><br><br clear="all"><br>-- <br>--<br>AnotherNetFellow<br>Email: <a href="mailto:anothernetfellow@gmail.com">anothernetfellow@gmail.com</a><br>
</div>