0

If you have one class using multiple inheritance using two classes with the same name for a function, which function takes priority? From what I found the function coming first in the class declaration takes priority. Is there a better way to call a function from the class listed second than what I have now?

class test():
    number = 10
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z
        self.something = z
    def printnumber(self):
        print(self.number)
class trial():
    def __init__(self,a,b,c):
        self.a = a
        self.b = b
        self.c = c
    def printnumber(self):
        print(self.c)
class check(test, trial):
    def __init__(self, a, b, c,x,y,z):
        trial.__init__(self ,a, b, c)
        test.__init__(self, x,y,z)
    def printnumber(self):
        return trial.printnumber(self)

Also, just curious, are there any other times in multiple inheretince or classes in general where the order that you list the classes in the class header matters?

Peean te
  • 3
  • 2

1 Answers1

-1

The search order is basically depth-first, left-to-right.

https://docs.python.org/3/tutorial/classes.html

However, because of the confusion it induces, it is generally bad form to write code like this. Multiple inheritance (beyond the very useful concept of a mix-in) is a dangerous practice that rarely pays in the long term.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30