python参数+关键字参数+inspect模块实例分析
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,这篇文章主要讲解了"python参数+关键字参数+inspect模块实例分析",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"python参数+关键字参数
千家信息网最后更新 2025年11月07日python参数+关键字参数+inspect模块实例分析
这篇文章主要讲解了"python参数+关键字参数+inspect模块实例分析",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"python参数+关键字参数+inspect模块实例分析"吧!
函数内省(function introspection)
除了__doc__属性, 函数对象还有很多属性,对于下面的函数,可以使用dir()查看函数具有的属性:
>>> dir(factorial) ['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
其中大多数是Python常规类都有的属性,下面重点看看常规对象没有而函数对象有的属性:
>>> class C:pass...>>> obj = C()>>> def func():pass...>>> sorted(set(dir(func)) - set(dir(obj))) # 计算差集,然后排序['__annotations__', '__call__', '__closure__', '__code__', '__defaults__', '__get__', '__globals__', '__kwdefaults__', '__name__', '__qualname__']
对于上面列出的函数特有属性,说明如下:
__annotations__ dict 参数和返回值的注释
__call__ method-wrapper 实现()运算符,即可调用对象的协议
__closure__ tuple 函数闭包,即自由变量的绑定(通常是None)
__code__ code 编译成字节码的函数元数据和函数定义体
__defaults__ tuple 形式参数的默认值
__get__ method-wrapper 实现只读描述符协议
__globals__ dict 函数所在的模块中的全局变量
__kwdefaults__ dict 仅限关键字形式参数的默认值
__name__ str 函数名称
__qualname__ str 函数的限定名称
定位参数和仅限关键字参数
def tag(name,*content,cls=None,**attrs):if cls is not None:attrs['class'] = clsif attrs:attrs_str = ''.join(' %s="%s" ' % (attr,value) for attr,value in sorted(attrs.items()))else:attrs_str=''if content:return '\n'.join('<%s %s >%s%s>' % (name,attrs_str,c,name) for c in content)else:return '<%s%s />' % (name,attrs_str)print(tag('br'))#定位参数 nameprint(tag('p','hello'))#hello 会被*conteng捕获 存入元组content = ('hello')print(tag('p','hello','world'))#content = ('hello','world')print(tag('p','hello',id=33)) #attrs={'id':33} content = ('hello')print(tag('p','hello','world',cls='sidebar'))#cls 关键字传入 cls='sidebar'print(tag(content='testing',name='img'))#第一个参数name 也能作为关键字传入#同名键会绑定到对应的具名参数上,剩余的则会被**attrs捕获print(tag(**{'name':'img','title':'sunset boulevard','src':'sunset.jpg','cls':'framed'}))#仅限关键字参数是python3.0新增的特性,在上例中,cls参数只能通过关键字参数指定,他一定不会捕获未命名的定位参数#定义函数时候,如果想指定仅限关键字参数,要把它们放到*的参数后面def f(a,*,b):return a,bff = f(1,b=2)print(ff)hello
hello
world
hello
(1, 2)
inspect模板
def tag(name,*content,cls=None,**attrs):if cls is not None:attrs['class'] = clsif attrs:attrs_str = ''.join(' %s="%s" ' % (attr,value) for attr,value in sorted(attrs.items()))else:attrs_str=''if content:return '\n'.join('<%s %s >%s%s>' % (name,attrs_str,c,name) for c in content)else:return '<%s%s />' % (name,attrs_str)import inspectsig = inspect.signature(tag)print(sig)my_tag = {'name':'img','title':'sun long','src':'sunlong.jpg','cls':'framed'}bound_args = sig.bind(**my_tag)for name,value in bound_args.arguments.items():print(name,'=',value)print(bound_args)inspect模块把实参绑定给函数调用:
(name, *content, cls=None, **attrs)name = imgcls = framedattrs = {'title': 'sun long', 'src': 'sunlong.jpg'}感谢各位的阅读,以上就是"python参数+关键字参数+inspect模块实例分析"的内容了,经过本文的学习后,相信大家对python参数+关键字参数+inspect模块实例分析这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
参数
函数
关键
关键字
模块
属性
实例
实例分析
分析
对象
学习
定位
内容
变量
名称
常规
形式
形式参数
自由
全局
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
数据库技术与应用实验五
电影服务器50m带宽能带多少
金铲铲扫码跳显示服务器已满
昆明服务器上门回收
网络技术在设备管理中的应用
群星先驱者数据库打不开
sonolus官网服务器
网络安全靠大家字体
业务逻辑写在数据库
软件开发工作任务描述
广州软件开发定制多少钱
校园网络安全保卫个人年终总结
鱼泡网软件开发
检察网络安全防范
工业实时数据库的意义
湖南软件开发专升本对口专业
在京东驻场软件开发累吗
金铲铲怎么切换服务器
数据库原理及应用知识点图
2014魔兽服务器
过节网络安全提示
小米软件开发待遇高吗
北大有小微企业数据库吗
安全服务器上禁止使用vac
阳江卫星软件开发费用是多少
互联网属于电子科技不
学生考勤系统数据库
asa写人数据库
机架式2u服务器
java jar包数据库
(1, 2)