<div>I have an update:</div><div><br></div><div>I can easily undertand why this example doesn'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't return any value.</div><div><br></div><div>But it'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 "local" list, isn't it? Maybe i can'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"><<a href="mailto:kent37@tds.net">kent37@tds.net</a>></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 <<a href="mailto:anothernetfellow@gmail.com">anothernetfellow@gmail.com</a>> wrote:<br>
<br>
> And, i have some difficulties understanding the other "strange" example in<br>
> that howto. Just scroll down to: "However, the point is that the value<br>
> of x is picked up from the environment at the time when the function is<br>
> defined. How is this useful? Let’s take an example — a function which<br>
> composes two other functions."<br>
> He is working on a function that compose other 2 functions. This is the<br>
> final solution<br>
> def compose(fun1, fun2):<br>
> def inner(x, fun1=fun1, fun2=fun2):<br>
> return fun1(fun2(x))<br>
> return inner<br>
> But also tries to explain why this example:<br>
> # Wrong version<br>
> def compose(fun1, fun2):<br>
> def inner(x):<br>
> return fun1(fun2(x))<br>
> return inner<br>
> def fun1(x):<br>
> return x + " world!"<br>
> def fun2(x):<br>
> return "Hello,"<br>
> sincos = compose(sin,cos) # Using the wrong version<br>
> x = sincos(3)<br>
> Won't work. Now, the problem is that the "inner" function gets fun1 and fun2<br>
> from other 2 functions.<br>
> My question is: why? inner is a sub-function of compose, where fun1 and fun2<br>
> 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 + " world!"<br>
...:<br>
<br>
In [8]: def fun2(x):<br>
...: return "Hello,"<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 "A Note About Python 2.1 and<br>
Nested Scopes" - 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>