如何在Python中在错误发生时重新启动装饰器

huangapple go评论61阅读模式
英文:

How to restart decorator on error in python

问题

我有一个名为func()的装饰器有时在运行时会引发AttributeError错误但如果重新运行装饰器一切都正常我尝试通过try except来修复它当出现错误1次时一切都正常但如果错误发生多次except不起作用
英文:

I have a func() decorator, sometimes it throws an error when running AttributeError, but if you re-run the decorator, then everything works, I tried to fix it through try except, when an error is thrown 1 time, everything works, but if the error is thrown several times, then except doesn't work

    try:
        func()
    except AttributeError:
        print('Error login, try again')
        func() ```

</details>


# 答案1
**得分**: 1

```python
while True:
    try:
        func()
        break
    except AttributeError:
        print('错误登录,请重试')
英文:
while True:
    try:
        func()
        break
    except AttributeError:
        print(&#39;Error login, try again&#39;)

huangapple
  • 本文由 发表于 2023年5月10日 17:07:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216674.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定