About lambda.

X

Xin Wang

Some c++ guru said c++ is hard to learn but easy to
use. Is python easy for both aspect? I found lot's of
confused in coding.

#code
from Tkinter import *

def on_click(m):
print m
def lamb_on_click(m):
return lambda : on_click(m)
root = Tk()
btns = ['0', '1', '2', '3']
for n in btns:
b = Button(root, text=n, command=lamb_on_click(n))
#Ok!
b.pack()
root.mainloop()
#

Or,

#code
....
b = Button(root, text=n, command=lambda x=n:
click(x)) #Ok!
....
#

The code above works well too, but follow is NOT! WHY?

#code
....
b = Button(root, text=n, command=lambda :
on_click(n))
....
#

Whichever button be clicked, this only print '3'.

Keyword 'lambda' is just like a macro in c or templete
in c++ or not?




__________________________________
Do you Yahoo!?
Yahoo! Mail Address AutoComplete - You start. We finish.
http://promotions.yahoo.com/new_mail
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top