python function call mechanism
function definition and call
https://www.programiz.com/python-programming/function-argument
函数参数定义有三种形式:
(1)固定位置参数
(2)可变参数
(3)任意参数
Arguments - 1 - 固定位置参数
只带有位置参数。
调用的时候, 传值也必须传入相同数量的参数值,于函数定义参数列表一一对应,否则抛出异常。
In the user-defined function topic, we learned about defining a function and calling it. Otherwise, the function call will result in an error. Here is an example.
def greet(name, msg): """This function greets to the person with the provided message""" print("Hello", name + ', ' + msg) greet("Monica", "Good morning!")
Variable Function Arguments - 可变参数
Up until now, functions had a fixed number of arguments. In Python, there are other ways to define a function that can take variable number of arguments.
Three different forms of this type are described below.
Python Default Arguments - 2 - 默认参数值定义
对于固定位置参数的函数, 使用上较为不便。
事实上,开发过程中,常常存在这种情况,希望参数有默认值, 调用时候并需要考虑给这种参数的传值; 遇到特殊情况再传值。
Function arguments can have default values in Python.
We can provide a default value to an argument by using the assignment operator (=). Here is an example.
def greet(name, msg="Good morning!"): """ This function greets to the person with the provided message. If the message is not provided, it defaults to "Good morning!" """ print("Hello", name + ', ' + msg) greet("Kate") greet("Bruce", "How do you do?")
Python Keyword Arguments - 调用时候,使用keyword传值
不管函数定义 是否为 固定位置参数, 还是可变参数;
调用时候, 也存在两种形式:
(1)按照固定位置传输参数值列表
(2)混合传值, 先传输 固定位置参数值列表(对应固定位置参数前缀), 然后使用keyword arguments传输剩余参数值。
这里给出第二种使用示例。
When we call a function with some values, these values get assigned to the arguments according to their position.
For example, in the above function
greet()
, when we called it asgreet("Bruce", "How do you do?")
, the value"Bruce"
gets assigned to the argument name and similarly"How do you do?"
to msg.Python allows functions to be called using keyword arguments. When we call functions in this way, the order (position) of the arguments can be changed. Following calls to the above function are all valid and produce the same result.
入下三种都是合法形式。
# 2 keyword arguments greet(name = "Bruce",msg = "How do you do?") # 2 keyword arguments (out of order) greet(msg = "How do you do?",name = "Bruce") 1 positional, 1 keyword argument greet("Bruce", msg = "How do you do?")
这种先传keyword arguments, 后传输 固定位参数, 是不合法的。
greet(name="Bruce","How do you do?")
Python Arbitrary Arguments - 3 - 任意参数
存在一种特殊情况,在函数被调用之前,我们不知道需要传输多少参数,
对于这种情况,则使用任意参数的函数定义方法。
Sometimes, we do not know in advance the number of arguments that will be passed into a function. Python allows us to handle this kind of situation through function calls with an arbitrary number of arguments.
In the function definition, we use an asterisk (*) before the parameter name to denote this kind of argument. Here is an example.
def greet(*names): """This function greets all the person in the names tuple.""" # names is a tuple with arguments for name in names: print("Hello", name) greet("Monica", "Luke", "Steve", "John")
传值机制
http://datasciencefree.com/python.pdf
如下图 function call mechanism第二点给了准确的执行过程:
假设f函数定义如, f(arg1, arg2, arg3, arg4=None)
对于函数调用语句, f(a, b, arg3=c)
(1) 首先python解析器, 扫描 实参列表 a, b, arg=c
生成固定位置参数值列表 , 一个元组(a, b)
生成一个keyword参数dict, 一个dict {‘arg3’: c}
(2)然后python解析器,找到f函数的定义, f(arg1, arg2, arg3, arg4=None)
先将固定位置参数列表, (a,b)赋值到形参 arg1 和 arg2, 等价于 arg1, arg2 = (a, b)
然后将keyword参数在形参表中寻找对应的参数,并把值赋值给形参, 这里是 arg3 = c
(3)赋值完毕,进行检查,如果形参中如果有参数还没有被赋值,则报错,否则运行函数体。