【发布时间】:2018-11-30 07:16:24
【问题描述】:
我想设计一个装饰器来检查任何函数注释类型,如果它具有相似的类型,则运行函数。 python可以做这样的事情吗? 如果python可以,请帮帮我!!
def foo (a:int):
if foo.__annotations__.get('a') == type(a):
pass
def boo (b:str):
if boo.__annotations__.get('b') == type(b):
pass
另一件事是注释是一个字典类型,我想要这样:
from type import FunctionType
def check (f:FunctionType):
result = True
k = [k for k in f.__annotations__.keys()]
v = [v for v in f.__annotations__.values()]
for i in range(len(v)):
if v[i] != type(k[i]): #but we don't access to the type of k[i] out of th f function
result = False
return result
【问题讨论】: