Программы на Python
-
Anonymous
Как аннотировать общий тип себя?
Сообщение
Anonymous »
У меня есть такие классы:
Код: Выделить всё
class TensorLike(ABC): ...
@property
@abstractmethod
def conj(self) -> 'TensorLike': ...
class Tensor(TensorLike): ...
_conj: 'Conjugate|None'=None
@property
def conj(self) -> 'Conjugate':
if self._conj is None:
self._conj = Conjugate(self)
return self._conj
class Conjugate(TensorLike): ...
_conj: Tensor
def __init__(self, value: Tensor): ...
self._conj = value
@property
def conj(self) -> Tensor:
return self._conj
T = TypeVar('T', bound=TensorLike, covariant=True)
class TensorNetwork(TensorLike, Collection[T]):
_conj: 'TensorNetwork[Any]|None' # TensorNetwork[Any]: #
Подробнее здесь: [url]https://stackoverflow.com/questions/79825047/how-to-annotate-with-the-generic-type-of-self[/url]
1763606334
Anonymous
У меня есть такие классы:
[code]class TensorLike(ABC): ...
@property
@abstractmethod
def conj(self) -> 'TensorLike': ...
class Tensor(TensorLike): ...
_conj: 'Conjugate|None'=None
@property
def conj(self) -> 'Conjugate':
if self._conj is None:
self._conj = Conjugate(self)
return self._conj
class Conjugate(TensorLike): ...
_conj: Tensor
def __init__(self, value: Tensor): ...
self._conj = value
@property
def conj(self) -> Tensor:
return self._conj
T = TypeVar('T', bound=TensorLike, covariant=True)
class TensorNetwork(TensorLike, Collection[T]):
_conj: 'TensorNetwork[Any]|None' # TensorNetwork[Any]: #
Подробнее здесь: [url]https://stackoverflow.com/questions/79825047/how-to-annotate-with-the-generic-type-of-self[/url]