# -*- coding:utf-8 -*-
'''带参数的装饰器,也就是根据参数定制化一个装饰器,可以看成生产装饰器的工厂
每次调用 typeassert,返回一个特定的装饰器,然后用它去装饰其它函数 '''
from inspect import signature
# 提取函数签名,只有在python3.5有该模块
def typeassert(*ty_args, **ty_kargs):
def decorator(func):
# func -> a, b
# d = {'a':int, 'b':str}
sig = signature(func)
btypes = sig.bind_partial(*ty_args, **ty_kargs).arguments
# 得到类型关系的映射,下面是bind_partial函数的注释
# Get a BoundArguments object, that partially maps the passed `args` and `kwargs` to the function's signature.
# Raises `TypeError` if the passed arguments can not be bound.
def wrapper(*args, **kargs):
# arg in d, instance(arg, d[arg])
for name, obj in sig.bind(*args, **kargs).arguments.items():
if name in btypes:
if not isinstance(obj, btypes[name]):
raise TypeError('"%s" must be "%s"' % (name, btypes[name]))
return func(*args, **kargs)
return wrapper
return decorator
@typeassert(int, str, list)
def f(a, b, c):
print(a, b, c)
f(1, 'abc', [1, 2, 3])
f(1, 2, [1, 2, 3])
9-3定义带参数的装饰器
最新推荐文章于 2025-10-13 20:44:04 发布
1万+

被折叠的 条评论
为什么被折叠?



