【发布时间】:2020-10-31 08:43:37
【问题描述】:
我是带有装饰器的字符串,我的第一个用途是包装一个 HTTP 调用以解决失败的连接。工作代码如下:
import requests
class Gotify:
def __init__(self):
self.url = "https://postman-echo.com/status/200"
def ensure_safe_call(caller):
def wrapper(*args, **kwargs):
try:
r = caller(*args, **kwargs)
r.raise_for_status()
except Exception as e:
try:
print(f"cannot reach gotify: {e}: {r.text}")
except NameError:
print(f"cannot reach gotify: {e} (the response r does not exist)")
else:
print("OK notified gotify of result change")
return wrapper
@ensure_safe_call
def send(self, title, message):
return requests.get(self.url)
Gotify().send("hello", "world")
这个正确显示OK notified gotify of result change。
在 PyCharm 中编辑此内容时,我收到两个我不理解的警告:
和
在我的装饰器上下文中它们是什么意思(当我不使用装饰器时没有)
【问题讨论】:
-
这个问题在这里已经有了答案:python-decorators-in-classes
标签: python python-3.x pycharm python-decorators