[Tutor] Minesweeper the return
Alberto Troiano
albertito_g at hotmail.com
Tue Jul 19 15:13:26 CEST 2005
Hey Danny
Thanks for the reply
Please let's talk about this more! *grin*
I'm not sure if I'm following you, what I see here is a function inside
other function and the first function returns its inside function, but how
does it work?
I don't know Scheme, but I know a little of C#, VB.NET, Pascal, QBasic and
C++ 6.0
But I would prefer to talk in theory or in general.
Thanks in advanced
Alberto
>From: Danny Yoo <dyoo at hkn.eecs.berkeley.edu>
>To: Alberto Troiano <albertito_g at hotmail.com>
>CC: tutor at python.org
>Subject: Re: [Tutor] Minesweeper the return
>Date: Mon, 18 Jul 2005 16:10:51 -0700 (PDT)
>
>
>
>On Mon, 18 Jul 2005, Alberto Troiano wrote:
>
>
> > I'm trying to create the buttons dinamically (that's not a problem) the
> > problem is wherever I click the hello function returns the last button I
> > created. I know that the problem is in the lambda part and that the
>variable
> > i (the one I change the value within the for) but I don't how to fix it
>
>
>Hi Alberto,
>
>Ah, this one comes up a bit. *grin* Let's look at the code:
>
>######
>for i in range(self.fil):
> for j in range(self.col):
> self.buttonmatrix[i][j]=Button(root,width=1,height=0,
> command=lambda: self.hello(i,j))
>######
>
>The issue is that all the dynamically generated functions are using the
>same 'i' and 'j' variables. The big issue is that, in Python, for-loops
>rebind the iterating variable name to different values, and that's what's
>causing the issue.
>
>
>Instead of using lambda directly, try something like this:
>
>######
>def make_hello(x, y):
> def new_hello_function():
> self.hello(x, y)
> return new_hello_function
>
>for i in range(self.fil):
> for j in range(self.col):
> self.buttonmatrix[i][j]=Button(root,width=1,height=0,
> command=make_hello(i, j))
>######
>
>
>There's some subtle lexical-binding stuff going on there, so if you'd
>like, we can talk about this more. If you're familiar with another
>programming language with lexical scope (like Scheme), we can talk about
>in terms of that language too.
>
>
>Hope this helps!
>
>_______________________________________________
>Tutor maillist - Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
Gaucho
More information about the Tutor
mailing list