Код: Выделить всё
class Complex(ABC):
@abstractmethod
def __complex__(self):
"""Return a builtin complex instance. Called for complex(self)."""
@abstractmethod
def __neg__(self):
"""-self"""
raise NotImplementedError
Код: Выделить всё
class MyComplex(Complex):
def __complex__(self):
return complex(1, 1)
def __neg__(self):
return complex(-1, -1)
Код: Выделить всё
>>> c = MyComplex()
>>> Complex.__complex__(c)
>>> Complex.__neg__(c)
Traceback (most recent call last):
File "", line 1, in
File "/home/maskalev/dev/foo/bar.py", line 15, in __neg__
raise NotImplementedError
NotImplementedError
Поэтому у меня есть несколько вопросов:
- Это единственная разница?
- Почему повышение NotImplementedError не используется в первом случае, а используется во втором? (Не лучше ли делать это единообразно?)
- Какова наилучшая практика? (Я думаю, что лучше явно вызвать исключение, но, возможно, я не вижу всей картины.)
Подробнее здесь: https://stackoverflow.com/questions/787 ... ct-methods