简体   繁体   English

我对这个 Python 代码背后的逻辑有一个简单的问题

[英]I have a simple question about the logic behind this Python code

I'm a beginner in Python, and I'm stuck in a function code.我是 Python 的初学者,我被困在一个函数代码中。

def max_of_two( x, y ):
    if x > y:
        return x
    return y
def max_of_three( x, y, z ):
    return max_of_two( x, max_of_two( y, z ) )
print(max_of_three(30, 25, 50))

Can someone explain to me the logic behind putting the first function (max_of_two()) inside the parameters of the second function (max_of_three())?有人可以向我解释将第一个函数 (max_of_two()) 放在第二个函数 (max_of_three()) 的参数中的逻辑吗? I've seen a function inside a function code, and that's not a problem, but I've never seen a function inside the parameters of another function... I'm very confused.我在函数代码中看到了一个函数,这不是问题,但是我从未在另一个函数的参数中看到一个函数......我很困惑。 I know what the code does, it basically shows the greater number.我知道代码的作用,它基本上显示了更大的数字。 The first function I understood perfectly, but the second one confused me...我完全理解第一个功能,但第二个让我感到困惑......

x = 1
y = 2
z = 3

max_of_two( y, z )
> 3

max_of_two( x, max_of_two( y, z ) )
# is the same as
max_of_two( x, z )
# is the same as
max_of_two( x, 3 )

The result of the inner function is used as a parameter for the outer function because the inner function is evaluated first.内部函数的结果用作外部函数的参数,因为首先评估内部函数。

This is not putting a function inside parameters.这不是将函数放入参数中。 First I recommend you understand parameter vs argument, here's a quote from "Parameter" vs "Argument" :首先,我建议您了解参数与参数,这是“参数”与“参数”的引用:

Old post, but another way of saying it: argument is the value/variable/reference being passed in, parameter is the receiving variable used w/in the function/block老帖子,但换一种说法:参数是传入的值/变量/引用,参数是函数/块中使用的接收变量

def max_of_three( x, y, z ):
    return max_of_two( x, max_of_two( y, z ) )

For example, (x, y, z) are parameters of max_of_three, and (y, z) are arguments passed to max_of_two例如,(x, y, z) 是 max_of_three 的参数,(y, z) 是传递给 max_of_two 的参数

—————————————————————————————————————————— Then you should understand function calls. ————————————————————————————————————————— 那你应该明白函数调用了。 max_of_two( y, z ) is an example of a function call, where you call the function max_of_two, by making a function call, you get the return value corresponding to your arguments. max_of_two( y, z ) 是函数调用的一个示例,您在其中调用函数 max_of_two,通过进行函数调用,您将获得与您的参数相对应的返回值。

In this case, when you write:在这种情况下,当您编写时:

max_of_two( x, max_of_two( y, z ) )

you first get the return value corresponding to (y, z) from max_of_two, and the pass x and the return value above to another max_of_two function, then you return the new return value from max_of_three.你先从 max_of_two 中得到对应于 (y, z) 的返回值,并将上面的 x 和返回值传递给另一个 max_of_two 函数,然后从 max_of_three 中返回新的返回值。 This is equivalent to:这相当于:

retval = max_of_two( y, z )
retval2 = max_of_two( x, retval )
return retval2

It's like a nested if in other languages.这就像其他语言中的嵌套 if。 You have three arguments to the second function.第二个函数有三个参数。 These are passed to the first function that verifies them in pairs.这些被传递给第一个成对验证它们的函数。 If you wanted to use a single function max_of_three(x, y, z) it should look like a succession of if statements with an intermediary variable.如果您想使用单个函数max_of_three(x, y, z)它应该看起来像带有中间变量的一系列 if 语句。

def max_of_three(x,y,z):
    if x > y:
       temp = x
    else:
       temp = y
    if temp > z:
       result = temp
    else:
       result = z
    return result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM