Python2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
>>> class ParentClass(object): ... def foo(self): ... print "call parent" ... >>> class ChildClass(ParentClass): ... def foo(self): ... super(ChildClass, self).foo() ... print "call child" ... >>> p = ParentClass() >>> c = ChildClass() >>> p.foo() call parent >>> c.foo() call parent call child |
Python3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
>>> class ParentClass(object): ... def foo(self): ... print("call parent") ... >>> class ChildClass(ParentClass): ... def foo(self): ... super().foo() # super()だけで良い。シンプル ... print("call child") ... >>> p = ParentClass() >>> c = ChildClass() >>> p.foo() call parent >>> c.foo() call parent call child |