Select to view content in your preferred language

How does multiple inheritance method works in?

5567
3
Jump to solution
03-24-2022 06:52 AM
rraahhaaddii09
New Contributor

I am doing my college assignment and I got stuck here, I am not understanding the this function in multiple inheritances.

 

 

 

# python 3 syntax
# multiple inheritance example
 
class parent1:                     # first parent class
    def func1:                   
        print(“Hello Parent1”)
 
class parent2:                     # second parent class
    def func2:                   
        print(“Hello Parent2”)
 
class parent3:                     # third parent class
    def func2:                     # the function name is same as parent2
        print(“Hello Parent3”)
 
class child(parent1, parent2, parent3):     # child class
    def func3:                     # we include the parent classes
        print(“Hello Child”)       # as an argument comma separated
                           
# Driver Code
test = child()        # object created
test.func1()          # parent1 method called via child
test.func2()          # parent2 method called via child instead of parent3
test.func3()          # child method called
 
# to find the order of classes visited by the child class, we use __mro__ on the child class
print(child.__mro__)

 

 

 

 

What I am not understanding is the code, can anyone explain to me how this code will work?

There are many articles on python that I am reading to complete my assignment and I am totally confused now.

Thanks for your help!

 

 

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

Hey, welcome to the ESRI community!

This is a forum specifically for all GIS products of ESRI. If you have nothing to do with that and only have questions about Python, this might not be the best place to ask.

 

  • a class can inherit from another class (simple inheritance)
  • a class can inherit from multiple classes (multiple inheritance)
  • inheritance means that the class has all attributes and methods of its parent class(es)
    • the class can define its own additional attributes and methods
    • the class can overwrite attributes and methods from its parent class(es)

In Python, the base classes of a class are stored in the __mro__ attribute (a tuple of classes). When you call a method or want to use an attribute, Python goes through this tuple from left to right and tries to find that method or attribute. If it doesn't find it, it goes to the next class. If it finds it, it returns the result, the other classes are not checked.

What this code example tries to teach you:

  • child inherits from 3 classes
  • you can call all methods of the parents on child
  • you can define additional methods on child
  • if 2 parent classes have methods/attributes with the same name, only the first will be returned
  • Python uses the __mro__ attribute to look for the methods and attributes. The order of the parent classes in this tuple determines which parent's method will be returned.

 

Your code samples has many errors. Here is the correct version:

class parent1:                     # first parent clas
    def func1(self):
        print("Hello Parent1")
 
class parent2:                     # second parent class
    def func2(self):                   
        print("Hello Parent2")
 
class parent3:                     # third parent class
    def func2(self):                     # the function name is same as parent2
        print("Hello Parent3")
 
class child(parent1, parent2, parent3):     # child class
    def func3(self):                     # we include the parent classes
        print("Hello Child")       # as an argument comma separated
                           
# Driver Code
test = child()        # object created
test.func1()          # parent1 method called via child
test.func2()          # parent2 method called via child instead of parent3
test.func3()          # child method called
 
# to find the order of classes visited by the child class, we use __mro__ on the child class
print(child.__mro__)

Have a great day!
Johannes

View solution in original post

3 Replies
by Anonymous User
Not applicable

I'd advise you to discuss this with your instructor. They need to know if content is not being understood by students so they can make adjustments to the courses lesson plans. Plus, even though items like this are pretty general, they could be looking for specific answers derived from the lesson content that we do not have access to which could cost you points.

JohannesLindner
MVP Frequent Contributor

Hey, welcome to the ESRI community!

This is a forum specifically for all GIS products of ESRI. If you have nothing to do with that and only have questions about Python, this might not be the best place to ask.

 

  • a class can inherit from another class (simple inheritance)
  • a class can inherit from multiple classes (multiple inheritance)
  • inheritance means that the class has all attributes and methods of its parent class(es)
    • the class can define its own additional attributes and methods
    • the class can overwrite attributes and methods from its parent class(es)

In Python, the base classes of a class are stored in the __mro__ attribute (a tuple of classes). When you call a method or want to use an attribute, Python goes through this tuple from left to right and tries to find that method or attribute. If it doesn't find it, it goes to the next class. If it finds it, it returns the result, the other classes are not checked.

What this code example tries to teach you:

  • child inherits from 3 classes
  • you can call all methods of the parents on child
  • you can define additional methods on child
  • if 2 parent classes have methods/attributes with the same name, only the first will be returned
  • Python uses the __mro__ attribute to look for the methods and attributes. The order of the parent classes in this tuple determines which parent's method will be returned.

 

Your code samples has many errors. Here is the correct version:

class parent1:                     # first parent clas
    def func1(self):
        print("Hello Parent1")
 
class parent2:                     # second parent class
    def func2(self):                   
        print("Hello Parent2")
 
class parent3:                     # third parent class
    def func2(self):                     # the function name is same as parent2
        print("Hello Parent3")
 
class child(parent1, parent2, parent3):     # child class
    def func3(self):                     # we include the parent classes
        print("Hello Child")       # as an argument comma separated
                           
# Driver Code
test = child()        # object created
test.func1()          # parent1 method called via child
test.func2()          # parent2 method called via child instead of parent3
test.func3()          # child method called
 
# to find the order of classes visited by the child class, we use __mro__ on the child class
print(child.__mro__)

Have a great day!
Johannes
rraahhaaddii09
New Contributor

Thanks, @JohannesLindner!

I will keep this "This is a forum specifically for all GIS products of ESRI" in mind.

My Apologies!

0 Kudos