简短的答案是 object.__init__() 方法除了检查是否未传入任何参数之外什么都不做。请参阅来源 http://hg.python.org/cpython/file/2.7/Objects/typeobject.c#l2847了解详情。
当调用一个实例时Service, the super() http://docs.python.org/library/functions.html#super调用将委托给 object.__init__() 并且什么也不会发生。
但是,当调用子类的实例时Service,事情变得更有趣了。这super()调用可能会委托给除object,一个类,它是实例的父类,但不是实例的父类Service。有关其工作原理以及为何有用的详细信息,请参阅博客文章Python 的超级 认为超级 http://rhettinger.wordpress.com/2011/05/26/super-considered-super/!
下面的例子(有点做作)展示了一个子类如何Service可以导致super打电话进来Service被引导到另一个名为Color:
class Service(object):
def __init__(self, host, binary, topic, manager, report_interval=None,
periodic_interval=None, *args, **kwargs):
print 'Initializing Service'
super(Service, self).__init__(*args, **kwargs)
class Color(object):
def __init__(self, color='red', **kwargs):
print 'Initializing Color'
self.color = color
super(Color, self).__init__(**kwargs)
class ColoredService(Service, Color):
def __init__(self, *args, **kwds):
print 'Initializing Colored Service'
super(ColoredService, self).__init__(*args, **kwds)
c = ColoredService('host', 'bin', 'top', 'mgr', 'ivl', color='blue')
在示例中,初始化按以下顺序进行:
- 初始化彩色服务
- 初始化服务
- 初始化颜色
- 初始化对象——除了参数检查之外什么都不做