Re: accumulator generators
- From: Hans Nowak <zephyrfalcon!NO_SPAM!@xxxxxxxxx>
- Date: Fri, 30 May 2008 18:58:09 -0400
Cameron wrote:
On May 30, 1:04 pm, "Diez B. Roggisch" <de...@xxxxxxxxxxxxx> wrote:Cameron schrieb:
I was reading this <a href="thishttp://www.paulgraham.com/icad.html">PaulBecause python's static analysis infers s as being a variable local to
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>
bar in the second case - so you can't modify it in the outer scope.
In the future, you may declare
def bar(i):
nonlocal s
...
Diez
thanks for the response. Just to make sure I understand- Is the reason
it works in the first case because s[0] is undefined at that point (in
bar), and so python looks in the outer scope and finds it there?
You can refer to variables in enclosing scopes, just not redefine them in that same scope. That's why in the first example, bar can refer to to s (defined in foo). By assigning to s[0], it modifies the list, which is OK; trying to redefine the name 's' (like the second example tries to do) would not be OK.
Also see: http://zephyrfalcon.org/labs/python_pitfalls.html (pitfall #6).
--
Hans Nowak (zephyrfalcon at gmail dot com)
http://4.flowsnake.org/
.
- References:
- accumulator generators
- From: Cameron
- Re: accumulator generators
- From: Diez B. Roggisch
- Re: accumulator generators
- From: Cameron
- accumulator generators
- Prev by Date: Re: Help needed in choosing an algorithm for Cryptographic services.
- Next by Date: Re: UNIX credential passing
- Previous by thread: Re: accumulator generators
- Next by thread: Re: accumulator generators
- Index(es):
Relevant Pages
|