What does Python fix?
Jason Orendorff
jason at jorendorff.com
Thu Jan 24 22:30:09 EST 2002
David Eppstein wrote:
> Tim Peters wrote:
> > This is what Smalltalk was striving for, but it failed to achieve a key
> > insight: nameless code blocks are easier to reuse if they're
> > named <wink>.
>
> But (unlike the unnamed lambda-forms) named code blocks are unable to
> access the local variables of some other code block.
I can't be 100% sure this is what you're referring to here,
but if you mean this sort of thing:
def cube_root(x):
""" Exercise 1.8 from SICP (Abelson and Sussman) """
def square(n):
return n * n
def cube(n):
return n * n * n
def good_enough(guess):
# NOTE reference to outer variable x
# and outer local function cube
return -0.000001 < cube(guess) - x < 0.000001
def improve_guess(y):
# NOTE reference to outer variable x
# and outer local function square
return (x / square(y) + 2 * y) / 3
guess = 1.0
while not good_enough(guess):
guess = improve_guess(guess)
return guess
It works in Python 2.2.
Also works in Python 2.1 if you add "from __future__ import nested_scopes".
## Jason Orendorff http://www.jorendorff.com/
More information about the Python-list
mailing list