[*] für ein beliebiges B Objekt x , f (x) ist vom Typ b ;
Für jeden C Objekt x , f (x) vom Typ c . < /li>
< /ul>
Ich möchte, dass eine Python -Code mit diesen Daten entspricht. Ich habe Folgendes ausprobiert: < /p>
Code: Select all
class A:
def __init__(self, args):
...
def f(self) -> A:
args = ... # things needing self
return A(args)
class B(A):
pass
class C(A):
pass
< /code>
und getestet < /p>
x = B(...)
y = x.f()
assert isinstance(y, B)
Code: Select all
return A(args)
von
Code: Select all
return type(self)(args)
Es scheint zu funktionieren; Ist es die bevorzugte Art, das Problem zu lösen?