Python'da Orta Düzey Nesne Yönelimli Programlama
Jake Roach
Data Engineer
Bir sınıfın birden fazla sınıfın işlevselliğini miras almasına olanak tanır
Intern, hem Employee hem Student’tan miras alabilir 
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):
# HER İKİ kurucuya da çağrı yapın
Employee.__init__(self, department)
Student.__init__(self, school)
self.duration = duration
def onboard(self, mentor):
# Yeni bir metodun uygulanması
...
stephen = Intern("Software Development", "Echo University", 10)
stephen.begin_job() # Employee’dan gelen metod
Welcome to Software Development!
stephen.add_course("Intermediate OOP in Python") # Intern’dan gelen metod
print(stephen.courses)
["Intermediate OOP in Python"]
Bir sınıf, başka bir sınıftan miras alan bir sınıftan miras alır; böylece torun olur

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)
Merhaba, adım Mike
Director of Engineering olarak yeni göreve başlıyor
14
Ebeveyn ve çocuklar aynı adlı bir metodu uyguladığında Python’un hangi metodu seçeceği sırası
MRO’yu belirleme kuralları:
$$
.mro() metodu ve __mro__

class Intern(Employee, Student): # Intern, Employee ve Student sınıflarını miras alır
...
# Intern için MRO’yu bulun
print(Intern.mro())
[<class '__main__.Intern'>, <class '__main__.Employee'>, <class '__main__.Student'>, <class 'object'>]
# __mro__ ile MRO’yu bulun
print(Intern.__mro__)
(<class '__main__.Intern'>, <class '__main__.Employee'>, <class '__main__.Student'>, <class 'object'>)
Python'da Orta Düzey Nesne Yönelimli Programlama