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

查看:119
本文介绍了对象的__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):

和服务的初始化方法定义:

and Service's init method definition:

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

并在Service的init中调用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天全站免登陆