使用 lambda 函数调用函数有什么作用?

huangapple go评论69阅读模式
英文:

What's the use of using a lambda function to call a function?

问题

如标题所示,我不知道使用lambda函数调用另一个函数的用途,尤其是将该lambda函数作为小部件(如按钮)的关键字参数时。

def multiply(x,y):
    print(x*y)

window = Tk()

x = 1
y = 1
button = Button(window, text='button', command=lambda x=x, y=y: multiply(x,y))
button.pack()

window.mainloop()

我一直在尝试自己操作它。我尝试不使用lambda函数来调用另一个函数,但它的效果不如我期望的那样。

英文:

So, as the title suggest, I don't know what's the use of using a lambda function to call another function, especially when putting that lambda function as a keyword argument in a widget such as a button.

def multiply(x,y):
    print(x*y)

window = Tk()

x = 1
y = 1
button = Button(window, text='button' command= lambda x=x, y=y : multiply(x,y))
button.pack()

window.mainloop()

I've been trying to play with it myself. I tried not to use a lambda function to call another function but it's not working as I expected it to be.

答案1

得分: 1

你想要的是,当你点击按钮时,会调用函数 multiply,并传入参数 "x" 和 "y"。

要实现这一点,你需要创建一个指向函数 multiply 的指针,并使用 lambda 表达式来实现。

如果你这样做:button = Button(window, text='button', command= multiply(x,y))

你并没有将一个指向函数的指针分配给 command 参数,而是直接调用了 multiply(x,y)

你可以尝试修改你的代码来验证这一点。

让我们看一个例子:

def print_value(i):
    print(i)

lambda_list = []
normal_list = []

for i in range(0,4):
    normal_list.append(print_value(i))

如果你这样做,你会看到输出 0,1,2,3,因为我正在调用函数 print_value

现在如果我这样做:

def print_value(i):
    print(i)

lambda_list = []
normal_list = []

for i in range(0,4):
    lambda_list.append(lambda x=i : print_value(x))

什么都不会被打印,因为我通过 lambda 函数在我的列表中添加了函数指针,而不是直接调用函数。

但是现在,如果我继续我的例子,这样做:

for lambda_func in lambda_list:
    lambda_func()

会打印出 0,1,2,3,因为我现在正在调用我的函数指针,并传入了我给它的参数(通过 lambda x=i)。

现在最后一个例子,

如果我这样做:

lambda_list = []

for i in range(0,4):
    lambda_list.append(lambda: print_value(i))

for lambda_func in lambda_list:
    lambda_func()

结果会是什么?答案是:3 3 3 3

因为当你这样做 lambda x=i : print_value(x) 时,你捕获的是 "i" 的当前值,但当你这样做 lambda: print_value(i) 时,它总是引用相同的 "i"(for 循环中的那个),因此你可能需要像在你的情况下这样做 lambda x=x, y=y : multiply(x,y),而不是 lambda: multiply(x,y)

希望这清楚了。

英文:

What you want is that when you click on your button, your function multiply gets called with the argument "x" and "y"

So achieve this, you need to create a pointer to the function multiply with the right argument, which you can achieve using a lambda expression.

if you were to do button = Button(window, text='button' command= multiply(x,y))

you are not assigning a pointer to a function to the command argument, but instead directly calling multiply(x,y)

you can try this yourself by modify your code

Let's see an example

def print_value(i):
    print(i)


lambda_list = []
normal_list = []

for i in range(0,4):
    normal_list.append(print_value(i))

if you do this, you'll see that 0,1,2,3 gets printed, because I'm calling the function print_value

Now if I do this :

def print_value(i):
    print(i)


lambda_list = []
normal_list = []

for i in range(0,4):
    lambda_list.append(lambda x=i : print_value(x))

Nothing gets printed, because I added pointer to the function inside my list through lambda function, and not directly called the function

but now if I continue my example by doing this

for lambda_func in lambda_list:
    lambda_func()

0,1,2,3 gets printed because I'm now calling my function pointer with the argument I gave to it (through lambda x=i)

now last example

If I were to do

lambda_list = []

for i in range(0,4):
    lambda_list.append(lambda: print_value(i))


for lambda_func in lambda_list:
    lambda_func()

What would be the result? answer : 3 3 3 3

Because when you do lambda x=i : print_value(x) you are capturing the current value of " i " , but when you do lambda: print_value(i), it always references the same "i" (the one of the for loop), ence why you may need to do
lambda x=x, y=y : multiply(x,y) in your case instead of lambda: multiply(x,y)

Hope this is clear.

huangapple
  • 本文由 发表于 2023年6月5日 14:17:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定