Raised to indicate that an abstract method or operation has not been implemented.
The NotImplementedError is a built-in exception in Python that is typically raised when an abstract method or required functionality in a base class or interface has not been implemented by a subclass. It serves as a reminder for developers to provide the necessary method implementations in subclasses, especially when designing frameworks or reusable code.
# Wrong code that causes error
class Animal:
def speak(self):
raise NotImplementedError('Subclasses must implement this method')
class Dog(Animal):
pass
d = Dog()
d.speak() # Raises NotImplementedError# Correct code
class Animal:
def speak(self):
raise NotImplementedError('Subclasses must implement this method')
class Dog(Animal):
def speak(self):
return 'Woof!'
d = Dog()
print(d.speak()) # Outputs: Woof!