abcEasy Examples

Abstract Base Classes: define interfaces subclasses must implement

Getting started with abc

Basic import and usage of the abc module.

python
from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def area(self):
        pass

    @abstractmethod
    def perimeter(self):
        pass

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius

    def area(self):
        return 3.14159 * self.radius ** 2

    def perimeter(self):
        return 2 * 3.14159 * self.radius

c = Circle(5)
print(f"Area: {c.area():.2f}")
print(f"Perimeter: {c.perimeter():.2f}")

The abc module is part of Python's standard library. Abstract Base Classes: define interfaces subclasses must implement.

Common abc operations

Frequently used functions from the abc module.

python
# More abc examples
import abc

print(f"abc module loaded successfully")
print(f"Location: {abc.__name__}")
print(f"Has {len(dir(abc))} attributes")

These are the most commonly used features of the abc module.

Want to try these examples interactively?

Open Easy Playground