对于静态初始化,我经常使用以下习语:
def compute_answer() -> int:
if compute_answer.ret is None:
# Do stuff that only happens the first time
compute_answer.ret = 42
return compute_answer.ret
compute_answer.ret = None
然而,使用mypy进行类型检查会出现以下错误:
compute.py:2: error: "Callable[[], int]" has no attribute "ret"
compute.py:4: error: "Callable[[], int]" has no attribute "ret"
compute.py:5: error: "Callable[[], int]" has no attribute "ret"
compute.py:7: error: "Callable[[], int]" has no attribute "ret"
我可以翻译。请问需要翻译哪些内容?您提供的是一个HTML标签,其中只有一句话:如何抑制这些错误,特别是在本地(例如,只针对此函数/属性)?
@functools.cache
装饰器吗? - Samwise