-2

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?

4
  • 4
    Why not trying it out? Commented Sep 25, 2018 at 14:22
  • Is the print method of both class x and y throw an error ? Commented Sep 25, 2018 at 14:28
  • What is class c (or is that a typo and you meant class z)? Commented Sep 25, 2018 at 14:33
  • 2
    Possible duplicate of Python and order of methods in multiple inheritance Commented Sep 25, 2018 at 14:37

2 Answers 2

1

Check this site: https://pythonprogramminglanguage.com/multiple-inheritance/

And you are right, you separate with commas.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Is print method of both class x and y will throw an error ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.