繁体   English   中英

实例化python类但未定义__init __()方法时会发生什么情况?

[英]What is happening when a python class is instantiated but it does not have any __init__() method defined?

给定以下类声明:

class FactorizationModel(ModelWrapper, Saveable, Loader):

其中ModelWrapperSaveableLoaderFactorizationModel的其他基类

此类没有定义任何__init__方法 相反,它具有一些定义,包括

def predict(self, user, product):

def predictAll(self, user_product):

def userFeatures(self):

def productFeatures(self):

鉴于没有init方法:请解释以下构造函数调用中发生的情况。

从导入此类的某些客户端代码中:

    return FactorizationModel(model)

更新 OK,这里的第一个答案是正确的。 添加相关信息以使其变得清晰:

class ModelWrapper(object):
    # the following base class constructor is getting called implicitly..
    def __init__(self, model):
          ...

初始化程序是从父类继承的,已解决问题。 它有点被多重继承弄得一团糟,如果有冲突,它将仅使用第一个传递的超类。 例如:

class C(A, B): # initializer from A will be used
class C(B, A): # initializer from B will be used

Python中的所有类都继承自一个名为Object的基类,该基类将该类分配到内存中(现在称为Object;一个类的实例。)因此,那些没有初始化程序的类将调用该基类初始化程序

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM