我制作了一个简单的decorator,它基本上将修饰函数放在try...except
中。你知道吗
from functools import wraps
def try_except(on_exception=None, exception=Exception, *args, **kwargs):
from sys import stderr
def decorator(func):
@wraps(func)
def wrapper(*args1, **kwargs1):
try:
return func(*args1, **kwargs1)
except exception as e:
print(repr(e), file=stderr)
if on_exception is not None:
return on_exception(*args, **kwargs)
return wrapper
return decorator
然后,我尝试修饰以下函数,因此如果它引发ValueError
,它会调用自己:
@try_except(get_int, ValueError, "Please enter a valid integer!\n>>> ")
def get_int(prompt=">>> "):
return int(input(prompt))
但我得到以下错误:
Traceback (most recent call last):
File "C:\Python\decorator_test.py", line 9348234, in <module>
@try_except(get_int, ValueError, "Please enter a valid integer!\n>>> ")
NameError: name 'get_int' is not defined
我知道我可以把它和函数中的try...except
放在一个while
循环中,但我这样做是为了学习。有什么办法不让这种事发生吗?你知道吗
一种方法是定义并使用sentinel对象来表示“call the same修饰的函数正在执行。即,在
def try_except
前面加上:在包装器的主体中,在
try
/except
之后:然后,修饰的函数将是,例如(我假设python3,假设您使用
input
的方式在python2中无疑是raw_input
)…:解决这个问题的最佳方法是使用一个调用函数的函数。你知道吗
因为
_get_int
返回调用时get_int
返回的内容(即运行时),它将更改为当前的get_int
。你知道吗您可能认为您可以在没有
@
语法糖的情况下完成它,但这只会调用previous(未修饰)函数,因此它不会递归。你知道吗相关问题 更多 >
编程相关推荐