Python syntax in Lisp and Scheme
Alex Martelli
aleax at aleax.it
Sat Oct 4 14:06:17 EDT 2003
Lulu of the Lotus-Eaters wrote:
> grzegorz at pithekos.net (Grzegorz Chrupala) wrote previously:
> |shocked at how awkward Paul Graham's "accumulator generator" snippet is
> |in Python:
> |class foo:
> | def __init__(self, n):
> | self.n = n
> | def __call__(self, i):
> | self.n += i
> | return self.n
>
> Me too. The way I'd do it is probably a lot closer to the way Schemers
> would do it:
>
> >>> def foo(i, accum=[0]):
> ... accum[0]+=i
> ... return accum[0]
> ...
> >>> foo(1)
> 1
> >>> foo(3)
> 4
>
> Shorter, and without an awkward class.
There's an important difference: with your approach, you cannot just
instantiate multiple independent accumulators like with the other --
a = foo(10)
b = foo(23)
in the 'class foo' approach, just as in all of those where foo returns an
inner-function instance, a and b are now totally independent accumulator
callables -- in your approach, 'foo' itself is the only 'accumulator
callable', and a and b after these two calls are just two numbers.
Making a cookie, and making a cookie-cutter, are quite different issues.
Alex
More information about the Python-list
mailing list