__init__ as a constructor?
潜入Python-
It would be tempting but incorrect to
call this the constructor of the
class. It's tempting, because it looks
like a constructor (by convention,
__init__ is the first method defined for the class), acts like one (it's
the first piece of code executed in a
newly created instance of the class),
and even sounds like one ("init"
certainly suggests a constructor-ish
nature). Incorrect, because the object
has already been constructed by the
time__init__ is called, and you
already have a valid reference to the
new instance of the class.
号
引号表明将
如果您有一个类
Foo() 为施工单位Foo.__init__() 是初始值设定项Foo.__new__() 是分配器-
__new__() is always called before__init__() . - First argument is the class itself which is passed implicitly.
-
Always return a valid object from
__new__() . Not mandatory, but thats the whole point.
小精灵
python对象的构造只是分配一个新实例,然后初始化该实例。
就我个人而言,我发现"
请求新对象时调用
在请求新对象时调用C++构造函数。它应该使用它的参数来分配给新对象上的字段,这样就可以设置对象正常操作所需的不变量。当构造函数中的代码开始运行时,对象已经是一个有效的预先存在的存储字段的位置。当构造函数中的代码开始运行时,新对象已经拥有其所有声明的字段,但它们包含垃圾。
当请求一个新对象时调用Java构造函数。它应该使用它的参数来分配给新对象上的字段,这样就可以设置对象正常操作所需的不变量。当构造函数中的代码开始运行时,对象已经是一个有效的预先存在的存储字段的位置。当构造函数中的代码开始运行时,新对象已经拥有其所有声明的字段,并具有它们的默认值。
EDCOX1的4种方法和C++/Java构造函数的主要区别在于我已经强调的最后一句话,这只是爪哇/C++的静态性质和Python的动态性质之间的区别。我认为这并不意味着要称它们为根本不同的概念,而不能用同一个词来指代它们。
我认为PythOnistas不喜欢引用EDCOX1 4作为构造函数的主要原因是人们认为C++ + Java构造函数是"创建一个新对象",因为这是他们在调用它们时所做的。但是当您调用一个构造函数时,实际上发生了两件事:创建一个新对象,然后调用该构造函数来初始化它。在C++/Java中,"创建一个新对象"的一部分是不可见的,而可以在Python中(通过EDCOX1×8的方法)进行曝光/定制。
因此,尽管EDCOX1 4的方法的作用与C++/Java构造函数的作用极为相似,但有些人倾向于强调这不是整个过程,而是说"EDCOX1×4是不是一个构造函数"。
构造函数返回一个实例,可能会失败。但
这里可以看到:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class A(object): def __init__(self): raise ValueError def __del__(self): print"Called" def main(): try: a = A() except ValueError, e: print"ValueError" if __name__ == '__main__': main() |
另一方面,
来自http://www.programmiz.com/article/python-self-why
__init__() is not a constructor[..]One important conclusion [..] is that,
__init__() is not a constructor. Many naive Python programmers get confused with it since__init__() gets called when we create an object. A closer inspection will reveal that the first parameter in__init__() is the object itself (object already exists). The function__init__() is called immediately after the object is created and is used to initialize it.Technically speaking, constructor is a method which creates the object itself. In Python, this method is
__new__() . A common signature of this method is
__new__(cls, *args, **kwargs)
号
关于本给出的答案,我会在这里说,大多数语言不遵循这个定义(完全)。
此外:
When
__new__() is called, the class itself is passed as the first argument automatically. This is what thecls in above signature is for. Again, likeself ,cls is just a naming convention. Furthermore,*args and**kwargs are used to take arbitary number of arguments during method calls in Python.Some important things to remember when implementing
__new__() are:
号
底线(对我来说):
约翰·泽尔在《编程Python:计算机科学导论》中说,"特殊方法