Python 为什么这行代码以这种方式工作?

Python 为什么这行代码以这种方式工作?,python,Python,注释下面一行所做的第一件事是调用函数divide。我很好奇,为什么会这样?这是因为python实际上理解操作顺序,还是因为此行具有链式结构?在调用函数之前,python必须计算传递给该函数的参数。(别无选择——参数值只有在已知的情况下才能传入。) 递归地应用这一原理,唯一的选择是首先调用divide()——在此之前,不知道其他函数的参数。在调用函数之前,Python必须计算传递给该函数的参数。(别无选择——参数值只有在已知的情况下才能传入。) 递归地应用这一原理,唯一的选择是首先调用divide

注释下面一行所做的第一件事是调用函数divide。我很好奇,为什么会这样?这是因为python实际上理解操作顺序,还是因为此行具有链式结构?

在调用函数之前,python必须计算传递给该函数的参数。(别无选择——参数值只有在已知的情况下才能传入。)


递归地应用这一原理,唯一的选择是首先调用divide()——在此之前,不知道其他函数的参数。

在调用函数之前,Python必须计算传递给该函数的参数。(别无选择——参数值只有在已知的情况下才能传入。)

递归地应用这一原理,唯一的选择是首先调用divide()——在此之前,没有其他函数的参数是已知的。

Python(以及任何带有函数的语言)理解函数调用的操作顺序

如果有一些函数f()、g()和h(),则

def add(a, b):
    print "ADDING %d + %d" % (a, b)
    return a + b

def subtract(a, b):
    print "SUBTRACTING %d - %d" % (a, b)
    return a - b

def multiply(a, b):
    print "MULTIPLYING %d * %d" % (a, b)
    return a * b

def divide(a, b):
    print "DIVIDING %d / %d" % (a, b)
    return a / b


print "Let's do some math with just functions!"

age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)

print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)



print "Here is a puzzle."

# why does the line of code below work in this way?
what = add(age, subtract(height, multiply(weight, divide(iq, 2))))

print "That becomes: ", what, "Can you do it by hand?"
需要h()的结果才能调用g(),在调用f()之前需要g(h())的结果

Python(以及任何带有函数的语言)理解函数调用的操作顺序

如果有一些函数f()、g()和h(),则

def add(a, b):
    print "ADDING %d + %d" % (a, b)
    return a + b

def subtract(a, b):
    print "SUBTRACTING %d - %d" % (a, b)
    return a - b

def multiply(a, b):
    print "MULTIPLYING %d * %d" % (a, b)
    return a * b

def divide(a, b):
    print "DIVIDING %d / %d" % (a, b)
    return a / b


print "Let's do some math with just functions!"

age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)

print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)



print "Here is a puzzle."

# why does the line of code below work in this way?
what = add(age, subtract(height, multiply(weight, divide(iq, 2))))

print "That becomes: ", what, "Can you do it by hand?"

需要h()的结果才能调用g(),在调用f()之前需要g(h())的结果

答案很简单-在调用函数之前,必须先计算所有函数参数

在这种情况下:

f(g(h()))
要计算
的值,请添加(…)
您必须计算
年龄的值

what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
要计算
减去(…)
的值,必须计算
高度的值

subtract(height, multiply(weight, divide(iq, 2)))

等等。

答案很简单-在调用函数之前,必须对所有函数参数进行求值

在这种情况下:

f(g(h()))
要计算
的值,请添加(…)
您必须计算
年龄的值

what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
要计算
减去(…)
的值,必须计算
高度的值

subtract(height, multiply(weight, divide(iq, 2)))

等等。

想想看。如果您不知道
subtract()
的结果是什么,您将如何调用
add()
?如果您不知道
multiply()
的结果是什么,您将如何调用
subtract()
?最后,如果您不知道
divide()
的结果是什么,您将如何调用
multiply()


与代数表示法一样,括号内的操作首先完成。如果括号内有括号,则首先执行最里面的操作。它不能以任何其他方式工作。

想想看。如果您不知道
subtract()
的结果是什么,您将如何调用
add()
?如果您不知道
multiply()
的结果是什么,您将如何调用
subtract()
?最后,如果您不知道
divide()
的结果是什么,您将如何调用
multiply()

与代数表示法一样,括号内的操作首先完成。如果括号内有括号,则首先执行最里面的操作。它不能以任何其他方式工作