[Tutor] Functions returning multiple values

Giorgio anothernetfellow at gmail.com
Mon Feb 22 15:36:44 CET 2010


Ahah Kent this is amazing.

I was reading the ITALIAN
http://www.python.it/doc/articoli/instpy-0.html version
of that guide that is not updated. But, when i decided to post there i've
posted the link of the guide in english, but actually that's not what i've
readen.

Ok, so in the new python version it works. Tha manual also states that: "The
features recognized by Python 2.6 are unicode_literals, print_function,
absolute_import, division, generators, nested_scopes andwith_statement.
generators, with_statement, nested_scopes are redundant in Python version
2.6 and above because they are always enabled."

Kent, as i'm learning py, can you please spend some words on nested scopes?
What are them? And why are supported anymore?

And, if i'm not asking you too much: can you plase post an example on how
that function can return HEllo World in py 2.6?

Thankyou!

Giorgio

2010/2/22 Kent Johnson <kent37 at tds.net>

> On Mon, Feb 22, 2010 at 9:13 AM, Giorgio <anothernetfellow at gmail.com>
> wrote:
>
> > And, i have some difficulties understanding the other "strange" example
> in
> > that howto. Just scroll down to: "However, the point is that the value
> > of x is picked up from the environment at the time when the function is
> > defined. How is this useful? Let’s take an example — a function which
> > composes two other functions."
> > He is working on a function that compose other 2 functions. This is the
> > final solution
> > def compose(fun1, fun2):
> >     def inner(x, fun1=fun1, fun2=fun2):
> >         return fun1(fun2(x))
> >     return inner
> > But also tries to explain why this example:
> > # Wrong version
> > def compose(fun1, fun2):
> >     def inner(x):
> >         return fun1(fun2(x))
> >     return inner
> > def fun1(x):
> >     return x + " world!"
> > def fun2(x):
> >     return "Hello,"
> > sincos = compose(sin,cos)  # Using the wrong version
> > x = sincos(3)
> > Won't work. Now, the problem is that the "inner" function gets fun1 and
> fun2
> > from other 2 functions.
> > My question is: why? inner is a sub-function of compose, where fun1 and
> fun2
> > are defined.
>
> It does work:
> In [6]: def compose(fun1, fun2):
>    ...:     def inner(x):
>   ...:         return fun1(fun2(x))
>   ...:     return inner
>    ...:
>
> In [7]: def fun1(x):
>   ...:         return x + " world!"
>   ...:
>
> In [8]: def fun2(x):
>   ...:         return "Hello,"
>   ...:
>
> In [9]: from math import sin, cos
>
> In [10]: sincos = compose(sin,cos)  # Using the wrong version
>
> In [11]:
>
> In [12]: x = sincos(3)
>
> In [13]:
>
> In [14]: x
> Out[14]: -0.8360218615377305
>
> That is a very old example, from python 2.1 or before where nested
> scopes were not supported. See the note "A Note About Python 2.1 and
> Nested Scopes" - that is now the default behaviour.
>
> Kent
>



-- 
--
AnotherNetFellow
Email: anothernetfellow at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100222/aecd139c/attachment.html>


More information about the Tutor mailing list