它实际上会以相反的方式工作 -ItemChild
的__init__
方法将首先被调用,并且只能转发适合的内容Item
:
class Item:
def __init__(self, param1):
print "Item.__init__ doing something with param1"
# do something with param1
class ItemChild(Item):
def __init__(self, param1, param2, param3):
print "Starting ItemChild.__init__"
super(ItemChild, self).__init__(param1)
print "ItemChild.__init__ doing something with params 2 and 3"
# do something with param2 and param3
调用时ItemChild(1, 2, 3)
的流程如下:
Starting ItemChild.__init__
Item.__init__ doing something with param1
ItemChild.__init__ doing something with params 2 and 3
不是:
Item.__init__ doing something with param1
Starting ItemChild.__init__
ItemChild.__init__ doing something with params 2 and 3
当然,您可以重新安排您的super
呼叫,使其在您做其他事情之前或之后运行 - 或者如果您不想让父母的行为运行,您可以完全忽略它。