admin管理员组

文章数量:1123943

I've seen this a lot online but with no discernable rhyme or reason. When creating an event binding, why call "lambda: thefunction() " instead of just passing " thefunction"? -- especially when nothing needs passed to said function?

ie. self.button = tk.Button(root, command= lambda:arb_func_call() vs. self.button = tk.Button(root, command= arb_func_call

is this a matter of style or is there a reason to use x over y?

I've seen this a lot online but with no discernable rhyme or reason. When creating an event binding, why call "lambda: thefunction() " instead of just passing " thefunction"? -- especially when nothing needs passed to said function?

ie. self.button = tk.Button(root, command= lambda:arb_func_call() vs. self.button = tk.Button(root, command= arb_func_call

is this a matter of style or is there a reason to use x over y?

Share Improve this question asked yesterday StarscreamStarscream 251 silver badge9 bronze badges 1
  • 1 When nothing is being passed in as an argument to arb_func_call, both approaches are equivalent. I prefer the command=arb_func_call because it's shorter and more readable to me but I use lambda if I am passing arguments to the callback function. – TheLizzard Commented yesterday
Add a comment  | 

1 Answer 1

Reset to default 0

There is no reason to use lambda for the command attribute of a button, unless you need the command to pass one or more arguments. The lambda with no arguments only serves to make the code more complicated than it needs to be.

本文标签: functionWhy use commandlambdasomefunction() vs commandsomefunction in button commandsStack Overflow