当 python class 被实例化但它没有定义任何 __init__() 方法时会发生什么?
What is happening when a python class is instantiated but it does not have any __init__() method defined?
给定以下 class 声明:
class FactorizationModel(ModelWrapper, Saveable, Loader):
其中ModelWrapper、Saveable、Loader是其他基础classes of FactorizationModel
此 class 没有定义任何 __init__ 方法 。相反,它有一些定义,包括
def predict(self, user, product):
def predictAll(self, user_product):
def userFeatures(self):
def productFeatures(self):
鉴于没有 init 方法:请解释以下构造函数调用中发生的情况。
来自一些导入此 class 的客户端代码:
return FactorizationModel(model)
更新 好的,这里的第一个答案是正确的。添加相关信息以明确这一点:
class ModelWrapper(object):
# the following base class constructor is getting called implicitly..
def __init__(self, model):
...
初始化器继承自父 class,问题已解决。它在多重继承中有点混乱,如果存在冲突,它只会使用第一个传递的 super class 。例如:
class C(A, B): # initializer from A will be used
class C(B, A): # initializer from B will be used
Python 中的所有 classes 都继承自一个名为 Object 的基 class,它将 class 分配到内存中(现在它被称为 Object;一个 class.) 因此那些没有初始化器的 classes 调用基础 class 初始化器
给定以下 class 声明:
class FactorizationModel(ModelWrapper, Saveable, Loader):
其中ModelWrapper、Saveable、Loader是其他基础classes of FactorizationModel
此 class 没有定义任何 __init__ 方法 。相反,它有一些定义,包括
def predict(self, user, product):
def predictAll(self, user_product):
def userFeatures(self):
def productFeatures(self):
鉴于没有 init 方法:请解释以下构造函数调用中发生的情况。
来自一些导入此 class 的客户端代码:
return FactorizationModel(model)
更新 好的,这里的第一个答案是正确的。添加相关信息以明确这一点:
class ModelWrapper(object):
# the following base class constructor is getting called implicitly..
def __init__(self, model):
...
初始化器继承自父 class,问题已解决。它在多重继承中有点混乱,如果存在冲突,它只会使用第一个传递的 super class 。例如:
class C(A, B): # initializer from A will be used
class C(B, A): # initializer from B will be used
Python 中的所有 classes 都继承自一个名为 Object 的基 class,它将 class 分配到内存中(现在它被称为 Object;一个 class.) 因此那些没有初始化器的 classes 调用基础 class 初始化器