Re: accumulator generators
- From: Arnaud Delobelle <arnodel@xxxxxxxxxxxxxx>
- Date: Sat, 31 May 2008 09:19:12 +0100
Cameron <cameronlarue@xxxxxxxxx> writes:
I was reading this <a href="this http://www.paulgraham.com/icad.html">Paul
Graham article</a> and he builds an accumuator generator function in
the appendix. His looks like this:
<pre>
def foo(n):
s = [n]
def bar(i):
s[0] += i
return s[0]
return bar
</pre>
Why does that work, but not this:
<pre>
def foo(n):
s = n
def bar(i):
s += i
return s
return bar
</pre>
Others have explained why, but this looks like "pythonized LISP" to
me. I would rather use a generator function:
def foo(n):
while True:
n += yield n
Although the problem is that you can't send it values the first time
round!
's'bar = foo('s')
bar.next()
'sp'bar.send('p')
'spam'bar.send('am')
But:
Traceback (most recent call last):bar = foo(3)
bar.send(2)
File "<stdin>", line 1, in <module>
TypeError: can't send non-None value to a just-started generator
--
Arnaud
.
- Follow-Ups:
- Re: accumulator generators
- From: George Sakkis
- Re: accumulator generators
- References:
- accumulator generators
- From: Cameron
- accumulator generators
- Prev by Date: Re: compatability
- Next by Date: Re: UNIX credential passing
- Previous by thread: Re: accumulator generators
- Next by thread: Re: accumulator generators
- Index(es):
Relevant Pages
|