Python函数func定义以下:前端
def func(first, *args, second="Hello World", **kwargs): print(first) print(args) print(second) print(kwargs) func("dongfanger", "san", py="good")
运行后会输出:python
dongfanger ('san',) Hello World {'py': 'good'}
它有四种参数:git
*args
是可变参数,arguments,存入元组。**args
是关键字参数,keyword arguments,存入字典。func函数的调用方式有如下这些:算法
①传入单个定位参数。segmentfault
func("dongfanger")
dongfanger () Hello World {}
②第一个参数后的任意个参数会被*args
捕获,存入一个元组。框架
func("dongfanger", "a", "b", "c")
dongfanger ('a', 'b', 'c') Hello World {}
③没有明确指定名称的关键字参数会被**kwargs
捕获,存入一个字典。函数
func("dongfanger", j="1", k="2")
dongfanger () Hello World {'j': '1', 'k': '2'}
④second只能做为关键字参数传入。工具
func("dongfanger", second="cool")
dongfanger () cool {}
⑤定位函数也能做为关键字参数传入。测试
func(first="san")
san () Hello World {}
⑥字典前加上**
,其全部元素做为单个参数传入,同名键会绑定到对应具名参数上,余下的被**args
捕获。优化
my_dict = {"first": "dongfanger", "location": "cd", "second": "cool", "age": "secret"} func(**my_dict)
dongfanger () cool {'location': 'cd', 'age': 'secret'}
除了这四种参数,还有一种Python3新增长的仅限关键字参数。
仅限关键字参数(keyword-only argument)是Python3的新特性,func函数的second参数就是仅限关键字参数,“仅限”的意思是说,只能经过关键字参数指定,它必定不会捕获未命名的定位参数。
假如把参数位置调整一下定义another_func函数:
def another_func(first, another_second="Hello World", *args, **kwargs): print(first) print(another_second) print(args) print(kwargs) another_func("dongfanger", "a", "b", "c")
输出会变成:
dongfanger a # 注意这里 ('b', 'c') {}
another_second不是仅限关键字参数,而只是默认值参数,由于它捕获到了定位参数。
由此得知,定义仅限关键字参数,必须把它放到*args
参数后面,就像func函数同样,反例是another_func函数。
还有第二个方法定义仅限关键字参数,在签名中放一个*
:
>>> def f(a, *, b): # b是仅限关键字参数 ... return a, b ... >>> f(1, b=2) # 只能传关键字参数 (1, 2) >>> f(1, 2) # 不能传定位参数 Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: f() takes 1 positional argument but 2 were given >>> f(1, 2, 3) # 不能传定位参数 Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: f() takes 1 positional argument but 3 were given
仅限关键字参数不必定要有默认值,就像b同样,强制必须传入实参。
函数内省的意思是说,当你拿到一个“函数对象”的时候,你能够继续知道,它的名字,参数定义等信息。这些信息能够经过函数对象的属性(一些双下划线的魔法方法)获得。
对于func函数:
def func(first, *args, second="Hello World", **kwargs): print(first) print(second) print(args) print(kwargs)
和another_func函数:
def another_func(first, another_second="Hello World", *args, **kwargs): print(first) print(another_second) print(args) print(kwargs)
【__defaults__
属性】
元组,保存着定位参数和关键字参数的默认值。
print(func.__defaults__) # None print(another_func.__defaults__) # ('Hello World',)
【__kwdefaults__
属性】
字典,保存仅限关键字参数。
print(func.__kwdefaults__) # {'second': 'Hello World'} print(another_func.__kwdefaults__) # None
【__code__
属性】
code对象引用,code对象自身有不少属性,其中包括参数名称。
print(func.__code__.co_varnames) # ('first', 'second', 'args', 'kwargs') print(another_func.__code__.co_varnames) # ('first', 'another_second', 'args', 'kwargs')
另外还能够使用inspect库的signature方法来查看内省中的函数参数:
from inspect import signature print(signature(func)) # (first, *args, second='Hello World', **kwargs)
框架和IDE等工具能够使用这些信息验证代码。
若是刷过力扣算法题,那么对函数注解就不会陌生。好比:
def clip(text:str, max_len:'int > 0'=80) -> str: pass
参数:
后面是注解表达式,能够用来注解参数类型和约束。若是参数有默认值,注解放在参数名和=号之间。
能够在函数末尾的)
和:
之间添加->
和注解表达式,来对返回值添加注解。
注解表达式能够是任何类型,最经常使用的类型是类(如str或int)和字符串(如'int > 0'
)。
函数注解只是个注解,Python对注解所作的惟一的事情是,把它们存入函数的__annotations__
属性中:
print(clip.__annotations__) #{'text': <class 'str'>, 'max_len': 'int > 0', 'return': <class 'str'>}
Python不作检查,不作强制,不作验证,什么操做都不作!注解只是元数据,能够供框架和IDE等工具使用。
本文介绍了Python函数的四种参数:定位参数、可变参数、默认值参数、关键字参数,和第五种Python3新特性参数:仅限关键字参数。拿到一个函数对象后,能够经过函数属性(一些双下划线的魔法方法)查看内省中的参数信息。函数注解是一种元数据,存在__annotations__
属性中,备注函数的参数和返回值的类型,它只是个注解,Python不会作任何强制检查。
参考资料:
《流畅的Python》
因为我的缘由,写文章时间变少了,虽然我仍然会努力码字,可是更文频率不得不下降。
从本周起公众号改周更了!固定周五早上七点半推送!细水流长不负读者指望!
时间跨度加长可能会致使内容连续性变弱,影视剧作法是设置上集回顾和下集预告,我借鉴了这个作法,在公众号文章最后附加了“更文进度“,告知之前更新了什么,之后会更新什么。
【上文回顾】
归档电子书:
https://dongfanger.gitee.io/blog/
【下文预告】
顺序不分前后,只列举我想到的可能会更新的内容: