Python Glossary Русский

all Python 3Exceptionsexception

NotImplementedError

built-in exceptions

Raised in abstract methods that subclasses are required to override. It shows that the method is deliberately left unimplemented.

Syntax

raise NotImplementedError('переопредели метод')

Do not confuse it with NotImplemented: that is a separate singleton which methods like __eq__ and __add__ return (never raise) so Python can try the reflected operation on the other operand. It is also a weak way to declare an abstraction — the stub only fires when the method is actually called, while abc.ABC with @abstractmethod refuses to instantiate an incomplete subclass at all. In the hierarchy NotImplementedError sits under RuntimeError.

Examples

class Shape:
    def area(self):
        raise NotImplementedError('area() must be implemented')
class Circle(Shape):
    def area(self):
        return 3.14 * self.r ** 2

See also

Open in the full glossarydocs.python.orgReport an inaccuracy