search

Home  >  Q&A  >  body text

Python newbie asks questions about formal parameters and actual parameters

code show as below:

# -*- coding:gb2312 -*-
def test(a,b,funC):
    result = funC(a,b)
    return result

funC = eval(input("请输入函数"))
num = test(11,22,funC)
print(num)

Execute as follows:

I input:
lambda x,y:x y
or
lambda x,y:x**y
, both will be successful.

My question is:
The letters after lambda are x and y, which are different from a and b in the previous funC(a,b)
Why can it also be true?

In other words, why shouldn't it be lambda a,b:a b? Shouldn't the letters I enter be the same as the letters above?

phpcn_u1582phpcn_u15822816 days ago1092

reply all(1)I'll reply

  • 扔个三星炸死你

    扔个三星炸死你2017-06-13 09:26:44

    Actual parameter (argument): The full name is "actual parameter" which is the parameter passed to the function when is called. Actual parameters can be constants, variables, expressions, functions , etc.
    Formal parameter (parameter): Full name It is a "formal parameter" because it is not an actual variable, so it is also called a dummy variable. It is the parameter used when defining the function name and function body.

    So

    a and b in lambda a,b:a+b are formal parameters and are used as local variables in the function body, while x and y are used as local variables in the function body. variables can be real Parameter . So it is used as the actual parameter of lambda function expression. It does not need to be the same.

    reply
    0
  • Cancelreply