def gcd(a, b):
'''Return the greatest common divisor of a and b.'''
a = abs(a)
b = abs(b)
if a < b:
a, b = b, a
while b != 0:
a, b = b, a % b
return a
def gcd(a: int, b: int) -> int:
>>> gcd.__annotations__
{'return': <class 'int'>, 'b': <class 'int'>, 'a': <class 'int'>}
>>> gcd.__annotations__['a']
<class 'int'>
def typecheck(f):
def wrapper(*args, **kwargs):
result = f(*args, **kwargs)
return_type = f.__annotations__.get('return', None)
if return_type and not isinstance(result, return_type):
raise RuntimeError("{} should return {}".format(f.__name__, return_type.__name__))
return result
return wrapper
Traceback (most recent call last):
File "typechecker.py", line 9, in <module>
gcd(1, 2)
File "typechecker.py", line 5, in wrapper
raise RuntimeError("{} should return {}".format(f.__name__, return_type.__name__))
RuntimeError: gcd should return int
def typecheck(f):
def wrapper(*args, **kwargs):
for i, arg in enumerate(args[:f.__code__.co_nlocals]):
name = f.__code__.co_varnames[i]
expected_type = f.__annotations__.get(name, None)
if expected_type and not isinstance(arg, expected_type):
raise RuntimeError("{} should be of type {}; {} specified".format(name, expected_type.__name__, type(arg).__name__))
result = f(*args, **kwargs)
return_type = f.__annotations__.get('return', None)
if return_type and not isinstance(result, return_type):
raise RuntimeError("{} should return {}".format(f.__name__, return_type.__name__))
return result
return wrapper
for name, arg in kwargs.items():
expected_type = f.__annotations__.get(name, None)
if expected_type and not isinstance(arg, expected_type):
raise RuntimeError("{} should be of type {}; {} specified".format(name, expected_type.__name__, type(arg).__name__))
def typecheck(f):
def wrapper(*args, **kwargs):
for i, arg in enumerate(args[:f.__code__.co_nlocals]):
name = f.__code__.co_varnames[i]
expected_type = f.__annotations__.get(name, None)
if expected_type and not isinstance(arg, expected_type):
raise RuntimeError("{} should be of type {}; {} specified".format(name, expected_type.__name__, type(arg).__name__))
for name, arg in kwargs.items():
expected_type = f.__annotations__.get(name, None)
if expected_type and not isinstance(arg, expected_type):
raise RuntimeError("{} should be of type {}; {} specified".format(name, expected_type.__name__, type(arg).__name__))
result = f(*args, **kwargs)
return_type = f.__annotations__.get('return', None)
if return_type and not isinstance(result, return_type):
raise RuntimeError("{} should return {}".format(f.__name__, return_type.__name__))
return result
return wrapper
def typecheck(f):
def do_typecheck(name, arg):
expected_type = f.__annotations__.get(name, None)
if expected_type and not isinstance(arg, expected_type):
raise RuntimeError("{} should be of type {} instead of {}".format(name, expected_type.__name__, type(arg).__name__))
@functools.wraps(f)
def wrapper(*args, **kwargs):
for i, arg in enumerate(args[:f.__code__.co_nlocals]):
do_typecheck(f.__code__.co_varnames[i], arg)
for name, arg in kwargs.items():
do_typecheck(name, arg)
result = f(*args, **kwargs)
do_typecheck('return', result)
return result
return wrapper
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有