创建实例时默认调用 __init__
Default call to __init__ when creating an instance
只是想要一些帮助来理解这些代码行:
class Parent:
def __init__(self):
print("instance created")
parent1=Parent()
parent2=Parent.__init__(parent1)
输出
instance created
instance created
我想了解如何在 OOP 中为 python 调用构造函数。
在第一行中,默认调用方法 __init__
并且传递的 self 参数不知何故 parent1
?
第二行是我认为调用方法的更传统的方式。由于 __init__
将 parent
class 的实例作为参数,我传递了 parent1
并且它有效。我知道第二行发生了什么,只是想问一下计算机在第一行创建实例parent1
。
__init__
相当于 Python 中的构造函数。将面向对象的语言想象成一种对表示对象方法的函数具有强制参数的语言,因此您始终可以访问该函数中的该对象。大多数语言不会让您按照输入 this
的方式输入。 Python 使用 self
,并让您为每个方法输入它。都是一样的,只是没有给你做额外的工作而已。
所以当Python实例化一个class时,它将class传递给class的__new__
函数,生成一个对象,然后将该对象作为第一个参数传递给 class 的 __init__
函数。
__init__
不是 构造函数 ,而是 初始化器 。当Python创建一个对象时,实际上是在__new__
中创建的(通常留为默认,这只是使右边class的一个空对象),它接收对class 和 returns 实例(通常为空;未设置属性)。结果实例作为 __init__
中的 self
隐式传递,然后建立实例属性。
通常,您不会直接调用像 __init__
这样的特殊方法(除了涉及具有合作继承的 super()
的情况),您只需让 Python 为您完成。 避免调用__init__
的唯一方法是显式调用class的__new__
(这也是非常不寻常的)。
你是对的,__init__()
像构造函数一样工作,在实例化对象时自动运行(如果有帮助的话,Java 构造函数会发生这种情况)。尽管您可以调用 __init__
,但您不应该以 _
或 __
开头调用 functions/methods,它们应该从 class/object 开始调用。
当 self
作为参数出现在 class 方法中时,您不必提供对象的名称,Python 会解决这个问题。所以上面第二行(Parent2 = ...
)不推荐
查看文档:
Called after the instance has been created (by __new__()
), but before it is returned to the caller. The arguments are those passed to the class constructor expression.
Called to create a new instance of class cls. __new__()
is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression
所以在 parent1 = Parent()
的幕后,Python 基本上是这样做的:
_temp_new_parent = Parent.__new__(Parent) # Inherited from "object.__new__"
Parent.__init__(_temp_new_parent)
parent1 = _temp_new_parent
(_temp_new_parent
并不存在,我只是将其用作抽象。)
请注意,__init__()
没有 return 任何内容,因此在您的代码中,parent2 is None
。如果 __init__()
设置了实例属性,它会在 parent1
上设置它们,因为那是你传入的。
只是想要一些帮助来理解这些代码行:
class Parent:
def __init__(self):
print("instance created")
parent1=Parent()
parent2=Parent.__init__(parent1)
输出
instance created
instance created
我想了解如何在 OOP 中为 python 调用构造函数。
在第一行中,默认调用方法 __init__
并且传递的 self 参数不知何故 parent1
?
第二行是我认为调用方法的更传统的方式。由于 __init__
将 parent
class 的实例作为参数,我传递了 parent1
并且它有效。我知道第二行发生了什么,只是想问一下计算机在第一行创建实例parent1
。
__init__
相当于 Python 中的构造函数。将面向对象的语言想象成一种对表示对象方法的函数具有强制参数的语言,因此您始终可以访问该函数中的该对象。大多数语言不会让您按照输入 this
的方式输入。 Python 使用 self
,并让您为每个方法输入它。都是一样的,只是没有给你做额外的工作而已。
所以当Python实例化一个class时,它将class传递给class的__new__
函数,生成一个对象,然后将该对象作为第一个参数传递给 class 的 __init__
函数。
__init__
不是 构造函数 ,而是 初始化器 。当Python创建一个对象时,实际上是在__new__
中创建的(通常留为默认,这只是使右边class的一个空对象),它接收对class 和 returns 实例(通常为空;未设置属性)。结果实例作为 __init__
中的 self
隐式传递,然后建立实例属性。
通常,您不会直接调用像 __init__
这样的特殊方法(除了涉及具有合作继承的 super()
的情况),您只需让 Python 为您完成。 避免调用__init__
的唯一方法是显式调用class的__new__
(这也是非常不寻常的)。
你是对的,__init__()
像构造函数一样工作,在实例化对象时自动运行(如果有帮助的话,Java 构造函数会发生这种情况)。尽管您可以调用 __init__
,但您不应该以 _
或 __
开头调用 functions/methods,它们应该从 class/object 开始调用。
当 self
作为参数出现在 class 方法中时,您不必提供对象的名称,Python 会解决这个问题。所以上面第二行(Parent2 = ...
)不推荐
查看文档:
Called after the instance has been created (by
__new__()
), but before it is returned to the caller. The arguments are those passed to the class constructor expression.
Called to create a new instance of class cls.
__new__()
is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression
所以在 parent1 = Parent()
的幕后,Python 基本上是这样做的:
_temp_new_parent = Parent.__new__(Parent) # Inherited from "object.__new__"
Parent.__init__(_temp_new_parent)
parent1 = _temp_new_parent
(_temp_new_parent
并不存在,我只是将其用作抽象。)
请注意,__init__()
没有 return 任何内容,因此在您的代码中,parent2 is None
。如果 __init__()
设置了实例属性,它会在 parent1
上设置它们,因为那是你传入的。