Misunderstanding about closures

fishboy fishboy at spamspamspam.com
Mon Jun 7 07:12:14 CEST 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"

Ok, how about this

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

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

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