Search notes:

Pyhon: multiple inheritance

Demo

class A:

      def __init__(self, num):
          print('A.__init__')
          self.num = num

      def method_a(self):
          print(f'A.method_a, num = {self.num}')

      def method_c(self):
          print('A.method_c')
      
      def method_d(self):
          print('A.method_d')

class B:

      def __init__(self, txt):
          print('B.__init__')
          self.txt = txt

      def method_b(self):
          print('B.method_b')

      def method_c(self):
          print('B.method_c')

      def method_d(self):
          print('B.method_d')

class CLS(A, B):

      def __init__(self, num, txt):
          print('CLS.__init__')

          A.__init__(self, num)
          B.__init__(self, txt)

      def method_d(self):
          print('CLS.method_d')

       #  A.method_d(self)
       #  B.method_d(self)

obj = CLS(42, 'Hello world')

obj.method_a()
obj.method_b()
obj.method_c()
obj.method_d()

print(isinstance(obj, A  )) # True
print(isinstance(obj, B  )) # True
print(isinstance(obj, CLS)) # True
Github repository about-Python, path: /class/derivation/multiple/demo.py

See also

Classes and class derivation
The __mro__ dunder.

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758206305, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Python/class/derivation/multiple/index(97): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78