mac*_*jan 17 python type-hinting mypy
以下代码无法按预期工作。显然,我不能在类定义中使用类自己的类型:
class Foo:
def __init__(self, key :str) -> None:
self.key = key
def __eq__(self, other :Foo) -> bool:
return self.key == other.key
print('should be true: ', Foo('abc') == Foo('abc'))
print('should be false: ', Foo('abc') == Foo('def'))
Run Code Online (Sandbox Code Playgroud)
运行结果如下:
Traceback (most recent call last):
File "class_own_type.py", line 1, in <module>
class Foo:
File "class_own_type.py", line 5, in Foo
def __eq__(self, other :Foo) -> bool:
NameError: name 'Foo' is not defined
Run Code Online (Sandbox Code Playgroud)
此外,检查代码mypy返回:
class_own_type.py:5: error: Argument 1 of "__eq__" incompatible with supertype "object"
Run Code Online (Sandbox Code Playgroud)
如何更正此代码以使其对 Python 和 for 有效mypy?
wim*_*wim 27
名称Foo尚未绑定,因为在您尝试使用名称时尚未定义类本身(请记住:函数参数在函数定义时计算,而不是在函数调用时计算)。
从 Python 3.7+ 开始,您可以通过在模块顶部添加此导入来推迟对注释的评估:
from __future__ import annotations
Run Code Online (Sandbox Code Playgroud)
在 Python 3.10 中,延迟评估将成为默认行为。对于 Python < 3.7,您可以使用字符串文字来延迟类型的评估:
class Foo:
def __init__(self, key :str) -> None:
self.key = key
def __eq__(self, other: 'Foo') -> bool:
return self.key == other.key
print('should be true: ', Foo('abc') == Foo('abc'))
print('should be false: ', Foo('abc') == Foo('def'))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7870 次 |
| 最近记录: |