多重継承の場合《第2版》
統合された概念モデル(Python 2.2 以降)に沿って、object の傘下に新たなクラスを定義すると、組み込み型/利用者定義クラスの違いを区別せずに扱えます。
##---------------------------------------- multiple inheritance class Father(object): def __init__(self): self.name = "Adam" def show(self): return "Madam, I'm %s"%self.name class Mother(object): def __init__(self): self.age = 23 def show(self): return "I'm %d years old"%self.age class Son(Father, Mother): def __init__(self, name, age): self.name = name self.age = age def show(self): return super(self.__class__, self).show() class Daughter(Mother, Father): def __init__(self, name, age): self.name = name self.age = age def show(self): return super(self.__class__, self).show()
以下のコードを実行すると、
print "-"*40 for e in [ Father(), Mother(), Son("John Doe", 0), Daughter("Jane Doe", 0), ]: print "%s: %s"%(e.__class__.__name__, e.show())
次のような出力が得られます。
---------------------------------------- multiple inheritance Father: Madam, I'm Adam Mother: I'm 23 years old Son: Madam, I'm John Doe Daughter: I'm 0 years old
親クラスのメソッドを呼び出すときには、組み込み関数 Parent を利用できます。多重継承を認めているので、単に super などと記述できません。
《付録》ex_super.py
""" >>> ================================ RESTART ================================ >>> ---------------------------------------- Python 2.1 Jonh Doe My name is Jonh Doe Jane Doe, 23 My name is Jane Doe, and I'm 23 years old ---------------------------------------- Python 2.2 Jonh Doe My name is Jonh Doe Jane Doe, 23 My name is Jane Doe, and I'm 23 years old ---------------------------------------- multiple inheritance Father: Madam, I'm Adam Mother: I'm 23 years old Son: Madam, I'm John Doe Daughter: I'm 5 years old """ ##--------------------------------------- Python 2.1 class Parent: def __init__(self, name): self.name = name def __str__(self): return "%s"%self.name def show(self): return "My name is %s"%self.name class Child(Parent): def __init__(self, name, age): Parent.__init__(self, name) self.age = age def __str__(self): return "%s, %d"%( Parent.__str__(self), self.age) def show(self): return "%s, and I'm %d years old"%( Parent.show(self), self.age) print "-"*40,"Python 2.1" e = Parent("Jonh Doe") print e; print e.show() e = Child("Jane Doe", 23) print e; print e.show() ##---------------------------------------- Python 2.2 class Parent(object): def __init__(self, name): self.name = name def __str__(self): return "%s"%self.name def show(self): return "My name is %s"%self.name class Child(Parent): def __init__(self, name, age): super(Child, self).__init__(name) self.age = age def __str__(self): return "%s, %d"%( super(Child, self).__str__(), self.age) def show(self): return "%s, and I'm %d years old"%( super(Child, self).show(), self.age) print "-"*40,"Python 2.2" e = Parent("Jonh Doe") print e; print e.show() e = Child("Jane Doe", 23) print e; print e.show() ##---------------------------------------- multiple inheritance class Father(object): def __init__(self): self.name = "Adam" def show(self): return "Madam, I'm %s"%self.name class Mother(object): def __init__(self): self.age = 23 def show(self): return "I'm %d years old"%self.age class Son(Father, Mother): def __init__(self, name, age): self.name = name self.age = age def show(self): return super(self.__class__, self).show() class Daughter(Mother, Father): def __init__(self, name, age): self.name = name self.age = age def show(self): return super(self.__class__, self).show() print "-"*40,"multiple inheritance" for e in [ Father(), Mother(), Son("John Doe", 4), Daughter("Jane Doe", 5), ]: print "%s: %s"%(e.__class__.__name__, e.show()) ##