0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Python3] 多重継承 Mixinについて

Last updated at Posted at 2023-05-02

メモ:
Pythonの多重継承において、super().init()とすると、classを定義するときに一番左に書いた引数のclassの__init__()が呼ばれる。

test_mixin01.py
class Father:
  def __init__(self):
    self.param="messages from father"
  def print_messages(self):
    print(self.param)

class Mother:
  def __init__(self):
    self.param="messages from mother"
  def print_messages(self):
    print(self.param)

class Son(Father, Mother): #see this line
  def __init__(self):
    super().__init__()
    self.print_messages()

class Daughter(Mother, Father): # see this line
  def __init__(self):
    super().__init__()
    self.print_messages()

def main():
  son=Son()
  daughter=Daughter()

if __name__ == "__main__":
  main()

実行結果はこちら

% python test_mixin01.py 
messages from father
messages from mother

より詳しくみるために、4つの親を持つclass Kidsを動かしてみる。

test_mixin03.py
class Father:
  def __init__(self):
    self.param="messages from father"
  def print_messages(self):
    print(self.param)

class Mother:
  def __init__(self):
    self.param="messages from mother"
  def print_messages(self):
    print(self.param)

class GrandFather:
  def __init__(self):
    self.param="messages from grand father"
  def print_messages(self):
    print(self.param)

class GrandMother:
  def __init__(self):
    self.param="messages from grand mother"
  def print_messages(self):
    print(self.param)

class Kids(Father, Mother, GrandFather, GrandMother): #see this line
  def __init__(self):
    super(Kids, self).__init__() #see this line
    self.print_messages()
    super(Father, self).__init__() #see this line
    self.print_messages()
    super(Mother, self).__init__() #see this line
    self.print_messages()
    super(GrandFather, self).__init__() #see this line
    self.print_messages()

def main():
  kids=Kids()
  print(Kids.mro()) #see this line

if __name__ == "__main__":
  main()

結果はこちら、

% python test_mixin03.py
messages from father
messages from mother
messages from grand father
messages from grand mother
[<class '__main__.Kids'>, <class '__main__.Father'>, <class '__main__.Mother'>, <class '__main__.GrandFather'>, <class '__main__.GrandMother'>, <class 'object'>]

多重継承をするとclassを定義するときの引数の順番でメソッドの解決が行われる。メソッドの解決順序は.mro()で調べることが可能。
上の例だと、

Kids -> Father -> Mother -> GrandFather -> GrandMother

となる。
例えば、Motherの__init__()を実行したいのであれば、Motherを継承しているFatherをsuper()の引数に指定する。つまり

super(Father, self).__init__() 

とする。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?