如何键入一个返回当前类实例的函数?

2024-09-30 22:16:32 发布

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

假设我有这些课程:

class GenericCopyable:
    def copy(self) -> GenericCopyable:
        ... # whatever is required to copy this

class CopyableFoo(GenericCopyable):
    ... # uses the parent implementation of "copy"
    def bar(self): …

def some_code(victim: CopyableFoo):
    v = victim.copy()
    v.bar()  ### I know that this works because "v" is a CopyableFoo, but mypy doesn't

问题是CopyableFoo.copy()的返回类型必须是CopyableFoo,而不是GenericCopyable

可能吗

编辑:以上是示例代码来说明问题。在本例中,以某种方式修改some_codeCopyableFoo当然是可能的;在我的“真实”计划中,这将是非常困难的


Tags: selfisdefrequiredbarcodesomethis
2条回答

一种可能的解决方案是重写子类中的方法,然后使用指定自己实例的返回类型的子类方法调用超类方法

class GenericCopyable:
    def copy(self) -> GenericCopyable:
        ... # whatever is required to copy this

class CopyableFoo(GenericCopyable):
   def copy(self)->CopyableFoo:
       return super().copy()

另一种可能的解决方案是使用键入模块导入Union。这指定父类中的函数能够返回多种类型


from typing import Union

class GenericCopyable:
    def copy(self) -> Union[GenericCopyable,CopyableFoo]:
        ... # whatever is required to copy this

class CopyableFoo(GenericCopyable):
    #Call parent class method directly
    GenericCopyable.copy()

你可以这样做

from typing import TypeVar
# We define T as a TypeVar bound to the base class GenericCopyable
T = TypeVar('T', bound='GenericCopyable')

class GenericCopyable:
    # we return the type T of the type of self
    # Basically returning an instance of the calling
    # type's class
    def copy(self: T) -> T:
        return type(self)()

class CopyableFoo(GenericCopyable):
    pass

foo = CopyableFoo()

bar = foo.copy()
print(bar)

这看起来有点笨拙,因为通常我们不需要注释self,因为它隐式地是它绑定到的类的一种类型。 然而,mypy似乎对此没有意见

相关问题 更多 >