Python类型在方法中暗示自己的类

2024-04-20 14:53:58 发布

您现在位置:Python中文网/ 问答频道 /正文

Edit:我注意到有人评论说类型提示不应该与__eq__一起使用,当然不应该。但这不是我问题的重点。我的问题是为什么类不能在方法参数中用作类型提示,而可以在方法本身中使用


在使用PyCharm时,Python类型暗示对我来说非常有用。然而,在尝试在其方法中使用类自己的类型时,我遇到了我觉得奇怪的行为

例如:

class Foo:

    def __init__(self, id):
        self.id = id
        pass

    def __eq__(self, other):
        return self.id == other.id

在这里,键入other.时,不会自动提供属性id。我希望通过如下定义__eq__来解决这个问题:

    def __eq__(self, other: Foo):
        return self.id == other.id

然而,这给了NameError: name 'Foo' is not defined。但是,当我在方法中使用类型时,在写入other.之后会提供id

    def __eq__(self, other):
        other: Foo
        return self.id == other.id

我的问题是,为什么不能使用类自己的类型来暗示参数,而在方法中是可能的


Tags: 方法selfid重点类型参数returnfoo
2条回答

名称Foo尚不存在,因此需要使用'Foo'。(mypy和其他类型检查器应将其识别为正向引用。)

def __eq__(self, other: 'Foo'):
    return self.id == other.id

或者,您可以使用

from __future__ import annotations

这会阻止对所有注释进行求值,并将它们简单地存储为字符串以供以后参考。(这将是Python 3.10中的默认设置。)

最后,正如评论中也指出的那样,__eq__首先应该而不是。第二个参数应该是任意对象;如果不知道如何将实例与之进行比较,则返回NotImplemented。(谁知道呢,也许知道如何将自己与您的实例进行比较。如果Foo.__eq__(Foo(), Bar())返回NotImplemented,那么Python将尝试Bar.__eq__(Bar(), Foo())。)

from typing import Any


def __eq__(self, other: Any) -> bool:
    if isinstance(other, Foo):
        return self.id == other.id
    return NotImplemented

或者用鸭子打字

def __eq__(self, other: Any) -> bool:
    # Compare to anything with an `id` attribute
    try:
        return self.id == other.id
    except AttributeError:
        return NotImplemented

在这两种情况下,Any提示都是可选的

由于您没有指定输入的类型,ide无法理解您正在处理的内容

尝试:

class Foo:

    def __init__(self, id):
        self.id = id
        pass

    def __eq__(self, other: Foo):
        return self.id == other.id

相关问题 更多 >