Sobes.tech
Junior — Middle

Which standard Python library provides creation and working with abstract classes?

sobes.tech AI

Answer from AI

The standard library abc (Abstract Base Classes) is used in Python for creating and working with abstract classes.

Example of usage:

from abc import ABC, abstractmethod

class Animal(ABC):
    @abstractmethod
    def sound(self):
        pass

class Dog(Animal):
    def sound(self):
        return "Woof"

# dog = Animal()  # Error: cannot instantiate abstract class

d = Dog()
print(d.sound())  # Will output: Woof

The abc module allows declaring abstract methods that must be implemented in subclasses.