Рассмотрим следующий код. У меня есть базовый и полученный класс, оба обратные данные, и я хочу вызвать метод базового класса в полученном классе через Super () :
import abc
import dataclasses
import typing
SLOTS = False
@dataclasses.dataclass(slots=SLOTS)
class Base:
@abc.abstractmethod
def f(self, x: int) -> int:
return x
@dataclasses.dataclass(slots=SLOTS)
class Derived(Base):
@typing.override
def f(self, x: int) -> int:
return super().f(x)
d = Derived()
d.f(2)
При настройке слотов = false , это работает нормально. При настройке слотов = true , я получаю ошибку:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[2], line 24
20 return super().f(x)
23 d = Derived()
---> 24 d.f(2)
Cell In[2], line 20, in Derived.f(self, x)
18 @typing.override
19 def f(self, x: int) -> int:
---> 20 return super().f(x)
TypeError: super(type, obj): obj (instance of Derived) is not an instance or subtype of type (Derived).
< /code>
Почему это? import abc
import typing
class Base:
__slots__ = tuple()
@abc.abstractmethod
def f(self, x: int) -> int:
return x
class Derived(Base):
__slots__ = tuple()
@typing.override
def f(self, x: int) -> int:
return super().f(x)
d = Derived()
d.f(2)
Подробнее здесь: https://stackoverflow.com/questions/794 ... eaks-super
Python @dataclasses (slots = true) ломает Super () ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Метод SUPER() в Python 3.11 — «Ошибка выполнения: super(): нет аргументов»
Anonymous » » в форуме Python - 0 Ответы
- 95 Просмотры
-
Последнее сообщение Anonymous
-