了解python中的嵌套Lambda函数行为 [英] Understanding nested lambda function behaviour in python

查看:205
本文介绍了了解python中的嵌套Lambda函数行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习纯函数式编程.但是这段代码让我特别困惑的是第二行.我不明白如何将值2传递给变量x.有人可以解释这种嵌套的 lambda 行为吗?

I'm trying to learn pure functional programming. But this code is puzzling me particularly the second line. I do not understand how the value 2 is passed to the variable x. Can somebody explain this nested lambda behaviour?

>>> square_func = lambda x: x**2
>>> function_product = lambda F, m: lambda x: F(x)*m
>>> square_func(2)
4
>>> function_product(square_func, 3)(2)
12

推荐答案

内部lambda在调用外部lambda时创建一个函数.外部lambda返回此函数.然后使用参数2调用此函数.

The inner lambda creates a function when the outer lambda is called. The outer lambda returns this function. This function is then called with the argument 2.

这篇关于了解python中的嵌套Lambda函数行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆