Programmation Orientée Objet en Python
  • Back to Main Website
  • Home
  • Introduction: Histoire et Concepts
    • Introduction: Histoire et Concepts
    • Histoire de la programmation
    • Première Structuration des données
    • Naissance de la POO
    • Python: tout n’est qu’objet
    • Python : Simplicité des objets et performance sous-jacente
    • Classes en Python : Concepts fondamentaux

    • Travaux Pratiques
    • Construire sa propre Liste
    • Construire son propre DataFrame
  • Encapsulation, Héritage, Composition et Dunder
    • Encapsulation, Heritage, Composition et Dunder
    • Encapsulation en Python
    • Héritage en Python : Concept et intérêt
    • Héritage vs Composition
    • Méthodes Dunder en Python
    • Python call Method: A Fun Exploration

    • Travaux Pratiques
    • TP: Heritage avec le pricing d’option
    • TP : Ajouter des méthodes dunder à DataFrameSimple
    • TP : Étendre la classe Liste avec des méthodes dunder
    • TP: Dunder Method with Tensor for Automatic Differentiation
  • Polymorphisme et Surcharge
    • Polymorphisme et Surcharge
    • Polymorphism in Object-Oriented Programming
    • Polymorphism in Python: Function Overloading and Type Checking
    • Class Creation: Standard vs type()
    • Type Hinting, Typing Module, and Linters in Python
    • Abstract Classes
    • Protocol Classes

    • Travaux Pratiques
    • TP
  • Decorators
    • Design Patterns
    • The decorator pattern
    • Decorator Practically
    • Built-in Decorators and Standard Library Decorators in Python
    • Practical Decorators in Python Libraries

    • Travaux Pratiques
    • TP: Monte Carlo Option Pricing with Decorators
    • TP: Optimizing Heston Model Monte Carlo Simulation
  • Project Management and Packaging
    • Project and Package
    • Organizing Python Projects
    • Understanding imports
    • Python Package Management and Virtual Environments
    • Unit Testing in Python

    • Travaux Pratiques
    • TP: Creating a Linear Regression Package
  • Design Patterns
    • OOP Design Patterns
    • Python-Specific Design Patterns
    • Creation Design Patterns
    • Structural Design Patterns
    • Behavioral Design Pattern

    • Travaux Pratiques
    • TP
  • Sujets de Projets possibles
    • Projets
    • Projets POO - 2024-2025
  • Code source
  1. Abstract Classes
  • Polymorphisme et Surcharge
  • Polymorphism in Object-Oriented Programming
  • Polymorphism in Python: Function Overloading and Type Checking
  • Class Creation: Standard vs type()
  • Type Hinting, Typing Module, and Linters in Python
  • Abstract Classes
  • Protocol Classes
  • Travaux Pratiques
    • TP

On this page

  • Abstract Classes in Python
    • Concept and Purpose
      • Key characteristics:
      • Purpose:
    • Abstract Base Classes (ABC) in Python
      • Basic Usage:
      • Implementing an Abstract Class:
      • Key Points:
    • Why Use Abstract Classes?
    • Example: Shape Hierarchy
    • Conclusion

Abstract Classes

Cours
Fondamentaux
Learn about abstract classes in Python, how to define and use them, and the benefits of using abstract classes for creating a common interface and enforcing method implementations in subclasses.
Author

Remi Genet

Published

2024-10-21

Abstract Classes in Python


Concept and Purpose

An abstract class is a class that is designed to be inherited from, but not instantiated directly. It often contains one or more abstract methods - methods that are declared, but don’t have an implementation in the abstract class.

Key characteristics:

  1. Cannot be instantiated directly
  2. May contain abstract methods (methods without a body)
  3. May contain concrete methods (methods with an implementation)
  4. Subclasses must implement all abstract methods

Purpose:

  1. To define a common interface for a set of subclasses
  2. To enforce certain methods to be implemented by subclasses
  3. To share code among several closely related classes

Abstract Base Classes (ABC) in Python

Python provides the abc module to work with abstract base classes.

Basic Usage:

In this example: - Animal is an abstract base class - make_sound is an abstract method - move is a concrete method

Implementing an Abstract Class:

Key Points:

  1. The @abstractmethod decorator marks a method as abstract
  2. Subclasses must implement all abstract methods
  3. Abstract classes can have both abstract and concrete methods
  4. Trying to instantiate an abstract class directly will raise a TypeError

Why Use Abstract Classes?

  1. Enforcing a common interface: Abstract classes ensure that all subclasses implement certain methods, guaranteeing a common interface.

  2. Code reuse: You can implement common functionality in the abstract base class, which all subclasses can use.

  3. Designing frameworks: Abstract classes are useful when designing large frameworks where you want to provide default behaviors but require specific implementations in subclasses.

Example: Shape Hierarchy

Here’s a more practical example using shapes:

In this example, Shape is an abstract base class that defines the common interface for all shapes. Circle and Rectangle are concrete implementations of Shape.

Conclusion

Abstract base classes in Python provide a powerful way to define interfaces and ensure that derived classes implement certain methods. They’re useful for creating frameworks and libraries, ensuring consistency across related classes, and defining clear contracts for subclasses to follow.

By using abstract classes, you can create more robust and well-structured code, especially when dealing with complex hierarchies of related classes.

Back to top
Type Hinting, Typing Module, and Linters in Python
Protocol Classes

Programmation Orienté Object en Python, Rémi Genet.
Licence
Code source disponible sur Github

 

Site construit avec et Quarto
Inspiration pour la mise en forme du site ici
Code source disponible sur GitHub