一个关于lambda的题目

据说是外国某个公司的题目。

那么这一坨代码到底是啥呢?

g = lambda x,y: lambda x: (-1 / y)* x + y

先print g, 原来是一个函数对象。

那么试试 g(0, 1)还是一个对象。

好吧,把它改造一下:

def otter_function(x, y):
    def inner_function(x):
        return (-1 / y) * x + y
    return inner_function

这样就比较好看了,然后加个函数参数,调用一下.

def otter_function(x, y):
    print locals()
    def inner_function(x):
        print locals()
        return (-1 / y) * x + y
    return inner_function

f = otter_function(0,1)
print f(1)
print f(2)
print f(3)

在这里,outter_function中,虽然传入了(0, 1)参数,但是内层函数里,x还是当成了参数,依然是一个抽象的值,而不是实际的值。

也就是x的数值并没有被带入。然后,把inner_function当成一个变量返回给outter_function.

最后,这是一条直线 x + y = 1, 那么和x轴夹角就知道了.

原文地址:https://www.cnblogs.com/jaw-crusher/p/3496537.html