Misunderstanding about closures

fishboy fishboy at spamspamspam.com
Mon Jun 7 01:12:14 EDT 2004


On Mon, 07 Jun 2004 03:27:22 GMT, "Alexander May"
<alex-news-deleteme at comcast.net> wrote:

>When I define a function in the body of a loop, why doesn't the function
>"close" on the loop vairable?  See example below.
>
Hmmm, I'm having a hard time phrasing the answer to this in a form
besides, "Because the function doesn't 'close' on a loop variable"

Hrmmm
.....
Ok, how about this

x = 'a'
for x in range(10):
	foo(x)
bar(x)

What value do you expect 'x' to be in bar(x)?

Because it sounds like you were expecting it to be bar('a') instead of
bar(9).

Hrmm, I just remembered.  That's a C thing, isn't it?  The loop
variable being only defined inside the loop.

God that makes to happy.  Forgetting stuff like that.  Maybe in
another 20 years I'll forget everything I ever knew about static typed
languages and pass into hacker Nirvana.

><{{{*>






More information about the Python-list mailing list