Suppose you have two classes x and y and each has a print method. If you have another class z and extend both x and y, then if you call print() from class z what will be happen?
2 Answers
Check this site: https://pythonprogramminglanguage.com/multiple-inheritance/
And you are right, you separate with commas.
Comments
Here the order of classes matters. If you write
class Z(X,Y)
then my_print method of class X will be executed.
and if you write class Z(Y,X) then my_print method of class Y will be executed.
note: there is built in print() function in python, please rename it. And class names should be Pascal case.
Example: this will output 'x':
class X:
def my_print(self):
print('x')
class Y:
def my_print(self):
print('y')
class Z(X, Y):
pass
my_object = Z()
my_object.my_print()
and this will output 'y'
class X:
def my_print(self):
print('x')
class Y:
def my_print(self):
print('y')
class Z(Y, X):
pass
my_object = Z()
my_object.my_print()
1 Comment
Rabbani_
Is print method of both class x and y will throw an error ?
c(or is that a typo and you meant classz)?