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!
Solved! Go to Solution.
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.
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:
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__)
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.
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.
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:
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__)
Thanks, @JohannesLindner!
I will keep this "This is a forum specifically for all GIS products of ESRI" in mind.
My Apologies!