Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我不理解下面函数的返回值 def horse(掩码): 马=面具 def面罩(马): 回程马 返回马(面具) 面具=羔羊马:马(2) 马(面具)_Python_Function_Currying - Fatal编程技术网

Python 我不理解下面函数的返回值 def horse(掩码): 马=面具 def面罩(马): 回程马 返回马(面具) 面具=羔羊马:马(2) 马(面具)

Python 我不理解下面函数的返回值 def horse(掩码): 马=面具 def面罩(马): 回程马 返回马(面具) 面具=羔羊马:马(2) 马(面具),python,function,currying,Python,Function,Currying,在Python导师上尝试了这段代码后,我感到更加困惑。 我需要有人更清楚地解释它。在我看来,代码是这样工作的: [掩码是参数so 2,此处显示] [因此2归于掩码] [参数中再次出现2] [返回2,因为该值已分配给第二行中的变量horse] [2在参数中] [掩码结果为2,因此函数返回2] 但它仍然非常混乱 我希望我能帮上忙 这段代码看起来很混乱,因为变量名经常被重用。 让我们通过改变变量名来解聚它 重命名内部掩码函数的参数: def horse(mask): horse = mask

在Python导师上尝试了这段代码后,我感到更加困惑。
我需要有人更清楚地解释它。

在我看来,代码是这样工作的:

[掩码是参数so 2,此处显示]

[因此2归于掩码]

[参数中再次出现2]

[返回2,因为该值已分配给第二行中的变量horse]

[2在参数中]

[掩码结果为2,因此函数返回2]

但它仍然非常混乱


我希望我能帮上忙

这段代码看起来很混乱,因为变量名经常被重用。 让我们通过改变变量名来解聚它

重命名内部掩码函数的参数:

def horse(mask):
    horse = mask
    def mask(x):
        return x
    return horse(mask)
重命名内部掩码函数本身:

def horse(mask):
    horse = mask
    def f(x):
        return x
    return horse(f)
现在,我们可以简化马的功能:

def horse(mask):
    def f(x):
        return x
    return mask(f)
同时重命名外部掩码函数参数:

mask = lambda y: y(2)

现在很容易看到,把你的代码作为文本包含在内,可以让谷歌搜索,这样也可以帮助其他人。你想实现什么?像这样重复使用作用域变量不是一个好的实践,您的代码本身就非常混乱。我很乐意帮忙,但是你的代码一定不是有意的脑筋急转弯。这个代码是在高阶函数主题下的讲座中给出的。所以我想我需要理解代码,但就像你说的,它绝对是一个脑筋急转弯。你发送一个函数,它发送一个函数,它执行一个值为2的函数。。。
return horse (mask) 

mask = lambda horse: horse (2) `[parameter for the function]`
horse (mask) 
def horse(mask):
    horse = mask
    def mask(x):
        return x
    return horse(mask)
def horse(mask):
    horse = mask
    def f(x):
        return x
    return horse(f)
def horse(mask):
    def f(x):
        return x
    return mask(f)
mask = lambda y: y(2)