Thanks Alan.  I prefer the lambda too.  Especially given how much code I
saved.  I forgot about i and j being dynamic and the call function.


On 1/25/13 4:14 PM, "Alan Gauld" <alan.ga...@btinternet.com> wrote:

>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/
>
>_______________________________________________
>Tutor maillist  -  Tutor@python.org
>To unsubscribe or change subscription options:
>http://mail.python.org/mailman/listinfo/tutor


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to