[Tutor] lambda
Alan Gauld
alan.gauld at btinternet.com
Sat Jan 26 01:14:05 CET 2013
On 25/01/13 23:57, anthonym wrote:
> I don't think my classmates will understand the use of lambda here but I
> am having are hard time converting that to strictly python.
lambdas are strictly python but they can be easily reanslated into a
named function as
lambda p: expr
becomes
def f(p):
return expr
so in your case
> b[i][j] = Button(font=('Aerial', 56), width=3, bg='yellow',
> command = lambda r=i,c=j: ttt(r,c))
becomes
def bCmd(r=i,c=j):
return ttt(r,c)
b[i][j] = Button(font=('Aerial', 56), width=3, bg='yellow',
command = bCmd)
Your problem of course is that you need i and j to be dynamically
defined so you need to create and call a function that returns a
function like this
def buttonFunMaker(i,j):
def func(x=i,y=j):
return ttt(x,y)
return func
b[i][j] = Button(font=('Aerial', 56), width=3, bg='yellow',
command = buttonFunMaker(i,j))
Personally I prefer the lambda...
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list