Multiple Inheritance

Intermediate Object-Oriented Programming in Python

Jake Roach

Data Engineer

Multiple inheritance

एक क्लास को एक से अधिक क्लास की फ़ंक्शनैलिटी इनहेरिट करने देता है

  • Intern दोनों Employee और Student से इनहेरिट कर सकता है
  • "is-a" संबंध बना रहता है

एक Intern क्लास, Employee और 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)
Intermediate Object-Oriented Programming in Python

Multiple inheritance

class Intern(Employee, Student):
    def __init__(self, department, school, duration):
        # Make a call to BOTH constructors
        Employee.__init__(self, department)
        Student.__init__(self, school)
        self.duration = duration

    def onboard(self, mentor):
        # Implementation of a new method
        ...
Intermediate Object-Oriented Programming in Python

Intern ऑब्जेक्ट बनाना

stephen = Intern("Software Development", "Echo University", 10)
stephen.begin_job()  # Method from Employee
Welcome to Software Development!
stephen.add_course("Intermediate OOP in Python")  # Method from Intern
print(stephen.courses)
["Intermediate OOP in Python"]
Intermediate Object-Oriented Programming in Python

Multilevel inheritance

किसी ऐसे क्लास से इनहेरिट करें जो खुद किसी और से इनहेरिट करता है, यानी ग्रैंडचाइल्ड बनना

  • "is-a" संबंध बनाए रखें

मल्टीलेवल इनहेरिटेंस दर्शाता एक ग्राफ़िक.

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
Intermediate Object-Oriented Programming in Python

Multilevel inheritance

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
Intermediate Object-Oriented Programming in Python

Method resolution order (MRO)

वह क्रम जिसमें Python तय करता है कि जब पैरेंट और चाइल्ड में एक ही नाम का मेथड हो तो कौन-सा मेथड चलेगा

MRO तय करने के लिए ये नियम मानें:

  • पहले चाइल्ड क्लास सर्च होगा
  • पैरेंट क्लासेस को class स्टेटमेंट में बाएँ-से-दाएँ परिभाषा के क्रम में सर्च किया जाएगा

$$

.mro() मेथड और __mro__

चाइल्ड क्लास कई पैरेंट क्लासेस से इनहेरिट कर रहा है.

1 Beyond the Basic Stuff with Python, Sweigart (pg. 311)
Intermediate Object-Oriented Programming in Python

Method resolution order (MRO)

class Intern(Employee, Student):  # Intern inherits the Employee and the Student classes
  ...

# Find the MRO for Intern
print(Intern.mro())
[<class '__main__.Intern'>, <class '__main__.Employee'>, <class '__main__.Student'>, <class 'object'>]
# Find the MRO using __mro__
print(Intern.__mro__)
(<class '__main__.Intern'>, <class '__main__.Employee'>, <class '__main__.Student'>, <class 'object'>)
Intermediate Object-Oriented Programming in Python

अभ्यास करते हैं!

Intermediate Object-Oriented Programming in Python

Preparing Video For Download...