对象的 __init__() 方法在 python 中有什么作用? [英] What does object's __init__() method do in python?

查看:36
本文介绍了对象的 __init__() 方法在 python 中有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读 OpenStack 的代码时,我遇到了这个.

While reading the code of OpenStack and I encountered this.

一个名为'Service'的类继承了基类'object',然后在Service的__init__()方法中,调用了对象的__init__.相关代码如下所示:

A class named 'Service' inherits the base class 'object', and then in Service's __init__() method, object's __init__ is called. The related code looks like this:

类定义:

class Service(object):

和Service的init方法定义:

and Service's init method definition:

def __init__(self, host, binary, topic, manager, report_interval=None,
             periodic_interval=None, *args, **kwargs):

并在服务的初始化中调用 super(此处的对象"):

and a call to super(the 'object' here) in Service's init:

super(Service, self).__init__(*args, **kwargs)

我不明白最后一次调用,object.__init__() 它实际上做了什么?有人可以帮忙吗?

I don't understand last call, object.__init__() what does it actually do? can anyone help?

推荐答案

简短的回答是 object.__init__() 方法除了检查没有传入参数之外什么都不做.参见 源码 了解详情.

The short answer is that object.__init__() method does nothing except check that no arguments have been passed in. See the source for details.

当在 Service 的实例上调用时,super() 调用将委托给 object.__init__() 并且什么都不会发生.

When called on an instance of Service, the super() call will delegate to object.__init__() and nothing will happen.

然而,当调用 Service 的子类的实例时,事情变得更有趣了.super() 调用可能会委托给除object 之外的某个类,该类是实例的父类,但不是Service 的父类>.有关其工作原理及其有用原因的详细信息,请参阅博客文章 Python的超级被认为是超级

However, when called on an instance of a subclass of Service, things get more interesting. The super() call can potentially delegate to some class other than object, a class that is a parent of the instance but not a parent of Service. For details on how this works and why it is useful, see the blog post Python's Super Considered Super!

以下示例(有些人为的)显示了 Service 的子类如何导致 Service 中的 super 调用被定向到另一个类称为颜色:

The following example (somewhat contrived) shows how a subclass of Service can cause the super call in Service to be directed to another class called 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')

在示例中,初始化按以下顺序进行:

In the example, initializations occur in the following order:

  1. 初始化彩色服务
  2. 初始化服务
  3. 初始化颜色
  4. 初始化对象——除了参数检查什么都不做

这篇关于对象的 __init__() 方法在 python 中有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆