假设您希望类 Bar 在其构造函数中设置值 34 ,这将起作用:
class Foo(object):
def __init__(self, frob, frotz):
self.frobnicate = frob
self.frotz = frotz
class Bar(Foo):
def __init__(self, frob, frizzle):
super(Bar, self).__init__(frob, frizzle)
self.frotz = 34
self.frazzle = frizzle
bar = Bar(1,2)
print "frobnicate:", bar.frobnicate
print "frotz:", bar.frotz
print "frazzle:", bar.frazzle
然而,super
引入了它自己的复杂性。参见例如super被认为是有害的。为了完整起见,这里是没有super
.
class Foo(object):
def __init__(self, frob, frotz):
self.frobnicate = frob
self.frotz = frotz
class Bar(Foo):
def __init__(self, frob, frizzle):
Foo.__init__(self, frob, frizzle)
self.frotz = 34
self.frazzle = frizzle
bar = Bar(1,2)
print "frobnicate:", bar.frobnicate
print "frotz:", bar.frotz
print "frazzle:", bar.frazzle