python - 类实例的类型
问题描述
我有以下代码,在 Python 3.9.2 上运行,我无法工作。
from__future__ import annotations
class P():
def __eq__(self:P, comparand:P) -> bool:
return false
Mypy抱怨说
Argument 1 of "__eq__" is incompatible with supertype "object";
supertype defines the argument types as "object"
关于如何做到这一点的任何建议?
解决方案
问题是eq方法适用于任何对象,没有 :P 它会起作用。您可以在代码中使用 isisinstance 检查参数的类型(如有必要)。
from__future__ import annotations
class P():
def __eq__(self, comparand) -> bool:
return false
推荐阅读
- python - 如何从python中的字符串中拆分一个字母
- laravel - 为每个 URL 路由单独的 Laravel 控制器?
- r - 更改基于 x 变量的箱线图背景 (ggplot2)
- javascript - 无法通过输入文件绘制导入的图像
- python - Matplot 显示一个空白矩形
- php - 将 Cloud SQL 中的数据导入/导出到 CSV 文件
- neo4j - 如何在neo4j中返回节点之前添加属性
- windows - 为 IIS 站点创建 Windows 用户,仅此而已
- r - 如何在 R 中的同一数据框中计算重叠的三天日志返回?
- javascript - 限制外部 CSS 对本地反应组件或 HTML 元素的影响