tgoop.com/DataScienceQ/1236
Create:
Last Update:
Last Update:
In Python, abstract base classes (ABCs) in the abc module define interfaces for subclasses to implement, enforcing polymorphism and preventing instantiation of incomplete classes. Use them for designing robust class hierarchies where specific methods must be overridden.
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
# rect = Rectangle(5, 3)
# print(rect.area()) # 15
# Shape() # Error: Can't instantiate abstract class
#python #OOP #classes #abc #inheritance
👉 @DataScience4
BY PyData Careers
Share with your friend now:
tgoop.com/DataScienceQ/1236
