python 函数定位参数+关键字参数+inspect模块

作者:wx59129d39de499 时间:2023-07-05 23:04:37 

函数内省(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'] = cls

if 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'))#定位参数 name
print(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,b
ff = f(1,b=2)
print(ff)
<br />
<p >hello</p>
<p >hello</p>
<p >world</p>
<p id="33" >hello</p>
<p class="sidebar" >hello</p>
<p class="sidebar" >world</p>
<img content="testing" />
<img class="framed" src="sunset.jpg" title="sunset boulevard" />
(1, 2)

inspect模板

def tag(name,*content,cls=None,**attrs):
if cls is not None:
attrs['class'] = cls
if 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 inspect
sig = 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 = img
cls = framed
attrs = {'title': 'sun long', 'src': 'sunlong.jpg'}
<BoundArguments (name='img', cls='framed', attrs={'title': 'sun long', 'src': 'sunlong.jpg'})>

来源:https://blog.51cto.com/u_12903656/5290389

标签:python,函数,定位,关键字,参数,inspect,模块
0
投稿

猜你喜欢

  • Python pymongo模块用法示例

    2022-05-23 20:00:21
  • python怎么去除字符串最后的换行符‘\\n’

    2022-06-12 21:15:24
  • 科讯CMS编辑器会自动更改代码

    2008-12-12 13:00:00
  • Python实现计算圆周率π的值到任意位的方法示例

    2021-09-08 16:47:09
  • vue2.0 elementUI制作面包屑导航栏

    2024-05-02 17:12:16
  • Python虚拟环境Virtualenv使用教程

    2022-04-15 00:22:21
  • 将python文件打包成EXE应用程序的方法

    2022-07-09 16:01:12
  • Laravel+Layer实现图片上传功能(整理篇)

    2024-05-03 15:28:59
  • CSS3 + HTML5 实现未来 Web 设计

    2010-01-25 12:17:00
  • 用Python进行websocket接口测试

    2022-03-02 09:44:22
  • CSS中背景background的一些语法

    2009-03-24 21:02:00
  • 浅谈Python的正则表达式

    2022-05-11 00:54:16
  • pytorch VGG11识别cifar10数据集(训练+预测单张输入图片操作)

    2021-10-22 21:55:52
  • php实现按照权重随机排序数据的方法

    2024-06-07 15:35:53
  • 看看如何用Python绘制小米新版天价logo

    2023-04-09 06:01:10
  • PHP详解ASCII码对照表与字符转换

    2023-11-07 01:16:55
  • uniapp小视频项目开发之滑动播放视频

    2023-07-02 05:24:36
  • python如何删除文件中重复的字段

    2021-09-19 15:43:12
  • python 判断字符串中是否含有汉字或非汉字的实例

    2022-07-01 11:25:59
  • 解决Jupyter 文件路径的问题

    2022-09-10 09:15:59
  • asp之家 网络编程 m.aspxhome.com