官网说明:https://docs.python.org/3/tutorial/controlflow.html#more-on-defining-functions
- 位置参数(positional argument)
主要从定义函数以及使用函数的两个角度理解;
使用时传递的实参既可以是位置参数,也可以关键字参数;
根据形参的位置为函数传递实参(如:day在前,weather在后)
# 定义位置参数
>>> def positional_argument_func(day, weather):
... print("{} is {}".format(day, weather))
# 为位置参数传递实参
>>> positional_argument_func("Sunday","sunny")
Sunday is sunny
- 关键词参数(keyword argument)
主要从使用函数的角度理解;
它实际上并没有具体的形参定义形式,通常共享位置参数的形参定义形式,通过“键=值”形式为函数传递实参;
关键词参数可以不按照形参定义的位置顺序来为函数传入实参;
需要特别注意:为函数传入实参时,关键字参数一定要放在位置参数之后!
# 定义位置参数(关键字参数无具体的形参定义形式)
>>> def keyword_argument_func(day, weather):
... print("{} is {}".format(day, weather))
# 为形参传入关键字参数
>>> keyword_argument_func(weather="sunny", day="Sunday")
Sunday is sunny
# 传入位置参数和关键字参数
>>> keyword_argument_func("Sunday", weather="sunny")
Sunday is sunny
# 错误的使用方式:使用函数传递实参时,关键词参数不能放在位置参数之前
>>> keyword_argument_func(weather="sunny", "Sunday")
File "<stdin>", line 1
SyntaxError: positional argument follows keyword argument
- 默认参数(default argument)
主要从定义函数的角度理解;
为形参设置默认值;
使用函数时,可以使用位置参数或关键字参数的实参形式,也不为函数传入具体的实参,此时函数就会自动使用默认值;
需要特别注意:定义函数时,默认参数不能放在位置参数之前;
# 定义默认参数
>>> def default_argument_func(day, weather="sunny"):
... print("{} is {}".format(day, weather))
...
# 错误的形参定义形式:默认参数不能放在位置参数之前
>>> def default_argument_func(day="Sunday", weather):
... print("{} is {}".format(day, weather))
...
File "<stdin>", line 1
SyntaxError: non-default argument follows default argument
# 传入位置参数
>>> default_argument_func("Monday", "rainy")
Monday is rainy
# 传入位置参数和关键字参数
>>> default_argument_func("Monday", weather="windy")
Monday is windy
# 传入关键字参数
>>> default_argument_func(day="Monday",weather="thundery")
Monday is thundery
从不严格的角度理解,默认参数和关键字参数可以视为一种参数,只不过默认参数只负责定义函数时的形式参数,而关键字参数只负责调用函数时的实参,两者都不能放在位置参数之前!
- 无关键字的变长参数(non-keyworded variable length argument)
主要从定义函数的角度理解;
允许向函数中传入可变数量的实参;
主要用在事先并不清楚需要给函数传递多少变量的场景;
使用方式参照位置参数;
# 简单实例 1
>>> def non_kvarg_func(*args):
... print(args)
...
>>> no_kvarg_func("Today","is","sunny")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'no_kvarg_func' is not defined
>>> non_kvarg_func("Today","is","sunny")
('Today', 'is', 'sunny')
# 简单实例 2
>>> def kvarg_func(day,*args):
... print(day+args)
...
>>> non_kvarg_func("Today","is","sunny")
('Today', 'is', 'sunny')
# 官方实例
>>> def concat(*args, sep="/"):
... print(sep.join(args))
...
>>> concat("earth","mars","venus")
earth/mars/venus
>>> concat("earth","mars","venus", sep=".")
earth.mars.venus
- 带关键字的变长参数(keyworded variable length argument)
主要从定义函数的角度理解;
允许向函数中传入可变数量的带关键字的实参;
主要用在事先并不清楚需要给函数传递多少变量的场景;
使用方式参照关键字参数;
# 简单实例
>>> def kvarg_func(day, *args, **kwargs):
... print(day)
... print(args)
... print(kwargs)
...
>>> kvarg_func("Sunday", "is", "or is not",weather1="sunny", weather2="rainy", weather3="windy")
Sunday
('is', 'or is not') # 无关键字的变长参数返回一个元组 tuple
{'weather1': 'sunny', 'weather2': 'rainy', 'weather3': 'windy'} # 带关键字的变长参数返回一个字典 dict
# 官方实例
>>> def cheeseshop(kind, *arguments, **keywords):
... print("-- Do you have any", kind, "?")
... print("-- I'm sorry, we're all out of", kind)
... for arg in arguments:
... print(arg)
... print("-"*40)
... for kw in keywords:
... print(kw, ":", keywords[kw])
...
>>> cheeseshop('Limburger', "It's vary runny, sir.", "It's really very, VERY runny, sir.", shopkeeper="Michael Palin", client="John Cleese", sketch="Cheese Shop Sketch")
-- Do you have any Limburger ?
-- I'm sorry, we're all out of Limburger
It's vary runny, sir.
It's really very, VERY runny, sir.
----------------------------------------
shopkeeper : Michael Palin
client : John Cleese
sketch : Cheese Shop Sketch