Programmation orientée objet intermédiaire en Python
Jake Roach
Data Engineer
Permet à une classe d'hériter des fonctionnalités de plusieurs classes
Intern peut hériter à la fois de Employee et de Student 
class Employee:
def __init__(self, department):
self.department = department
def begin_job(self):
print(f"Welcome to {self.department}!")
class Student:
def __init__(self, school):
self.school = school
self.courses = []
def add_course(self, course_name):
self.courses.append(course_name)
class Intern(Employee, Student):
def __init__(self, department, school, duration):
# Appel aux DEUX constructeurs
Employee.__init__(self, department)
Student.__init__(self, school)
self.duration = duration
def onboard(self, mentor):
# Implémentation d'une nouvelle méthode
...
stephen = Intern("Software Development", "Echo University", 10)
stephen.begin_job() # Méthode de Employee
Welcome to Software Development!
stephen.add_course("Intermediate OOP in Python") # Méthode de Intern
print(stephen.courses)
["Intermediate OOP in Python"]
Hériter d'une classe qui hérite elle‑même d'une autre classe : on devient le petit‑enfant

class Person:
def __init__(self, name):
self.name = name
def introduce(self):
print(f"Hello, my name is {self.name}")
class Employee(Person):
def __init__(self, name, title):
Person.__init__(self, name)
self.title = title
def change_position(self, new_title):
print(f"Starting new role as {new_title}")
self.title = new_title
class Manager(Employee):
def __init__(self, name, title, number_reports):
Employee.__init__(self, name, title)
self.number_reports = number_reports
mike = Manager("Mike", "Engineering Manager", 14)
mike.introduce()
mike.change_position("Director of Engineering")
print(mike.number_reports)
Hello, my name is Mike
Starting new role as Director of Engineering
14
L'ordre selon lequel Python détermine quelle méthode utiliser quand parent et enfants implémentent une méthode du même nom
Suivez ces règles pour déterminer le MRO :
$$
Méthode .mro() et __mro__

class Intern(Employee, Student): # Intern hérite des classes Employee et Student
...
# Trouver le MRO d'Intern
print(Intern.mro())
[<class '__main__.Intern'>, <class '__main__.Employee'>, <class '__main__.Student'>, <class 'object'>]
# Trouver le MRO avec __mro__
print(Intern.__mro__)
(<class '__main__.Intern'>, <class '__main__.Employee'>, <class '__main__.Student'>, <class 'object'>)
Programmation orientée objet intermédiaire en Python