python-如何使用函数注释来验证函数调用类型
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-如何使用函数注释来验证函数调用类型,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2178字,纯文字阅读大概需要4分钟。
内容图文
![python-如何使用函数注释来验证函数调用类型](/upload/InfoBanner/zyjiaocheng/685/d870c9cfa0d44ea19ed9e3b7344b647c.jpg)
我最近才发现有一种叫做函数注释的东西,但是我不太确定如何使用它.这是我到目前为止的内容:
def check_type(f):
def decorated(*args, **kwargs):
counter=0
for arg, type in zip(args, f.__annotations__.items()):
if not isinstance(arg, type[1]):
msg = 'Not the valid type'
raise ValueError(msg)
counter+=1
return f(*args, **kwargs)
return decorated
@check_type
def foo(a: int, b: list, c: str): #a must be int, b must be list, c must be str
print(a,b,c)
foo(12, [1,2], '12') #This works
foo(12, 12, 12) #This raises a value error just as I wanted to
foo(a=12, b=12, c=12) #But this works too:(
如您所见,我正在尝试使用批注和装饰器检查a,b和c的类型,如果类型不正确,则会引发ValueError.当我在调用时不使用关键字参数时,效果很好.但是,如果我使用关键字参数,则不会检查类型.我正在尝试使其正常运行,但是我没有运气.
我的代码不支持关键字参数.因为我没有任何可以检查的内容.我也不知道如何检查它.这是我需要帮助的地方.
我也是这样做的:
def check_type(f):
def decorated(*args, **kwargs):
for name, type in f.__annotations__.items():
if not isinstance(kwargs[name], type):
msg = 'Not the valid type'
raise ValueError(msg)
return f(*args, **kwargs)
return decorated
#But now they have to be assigned using keyword args
#so only foo(a=3,b=[],c='a') works foo(3,[],'a') results in a keyerror
#How can I combine them?
解决方法:
正如Paul所建议的,最好使用Signature对象(位于inspect中)的bind方法来绑定要提供给f的* args和** kwargs,然后检查类型是否匹配:
from inspect import signature
from typing import get_type_hints
def check_range(f):
def decorated(*args, **kwargs):
counter=0
# use get_type_hints instead of __annotations__
annotations = get_type_hints(f)
# bind signature to arguments and get an
# ordered dictionary of the arguments
b = signature(f).bind(*args, **kwargs).arguments
for name, value in b.items():
if not isinstance(value, annotations[name]):
msg = 'Not the valid type'
raise ValueError(msg)
counter+=1
return f(*args, **kwargs)
return decorated
您的第一个案例实际上是随机成功的.字典在Python中的顺序是随机的< 3.6可能会在您再次启动Python解释器时发生变化,这意味着您所做的压缩不是确定性的. 与其通过f .__ annotations__进行迭代,不如通过get_type_hints进行抓取,然后通过b.items()(它是OrderedDict并保证顺序)通过名称对它进行索引来获取名称和值.
内容总结
以上是互联网集市为您收集整理的python-如何使用函数注释来验证函数调用类型全部内容,希望文章能够帮你解决python-如何使用函数注释来验证函数调用类型所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。