python 中的构造函数和初始值设定项有什么区别?
Possible Duplicate:
Python (and Python C API): new versus init
I'm at college just now and the lecturer was using the terms constructors and initializers interchangeably. I'm pretty sure that this is wrong though.
I've tried googling the answer but not found the answer I'm looking for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在大多数 OO 语言中,它们是相同的步骤,因此对于 java、c++ 等,他并没有错。在 python 中,它们分两步完成:
__new__
是构造函数;__new__
是构造函数;__init__
是初始化器。这里是另一个答案,更详细地介绍了差异他们之间。
In most OO languages, they are the same step, so he's not wrong for things like java, c++, etc. In python they are done in two steps:
__new__
is the constructor;__init__
is the initializer.Here is another answer that goes into more detail about the differences between them.
在几乎所有常见情况下,Python 没有与其他 OO 语言使用的相同意义上的构造函数,因为通常不鼓励手动管理内存。相反,您通常应该做的是在类上定义一个 __init__ 方法。调用此方法来自动初始化新的实例对象,这是构造后的第一件事。因此,它并不是真正的构造函数,并且将其称为构造函数可能会让一些人感到困惑。
当然,有些人想将其称为构造函数,因为它的使用有点像构造函数 - 从根本上讲,只要每个人都理解您实际指的是什么,您可以随意称呼它。但一般来说,为了明确并让自己理解,请将其称为 init 方法或构造函数以外的其他方法。从根本上来说,不同的语言只是有一些不同的术语,要说得非常清楚总是需要根据你的主题和受众进行调整。
在Python中,可以以更细的粒度管理实例的创建和销毁,尽管除非您知道自己在做什么,否则您不会想要这样做。这是通过定义 __new__ 和 __del__ 方法来挂钩对象实例化和 del 语句来完成的。这些是否精确地限定为构造函数和析构函数还有一点争议(Python 文档将 del 方法称为析构函数,但对于构造函数的构成往往比较模糊,例如包括许多返回对象实例的函数)。我仍然鼓励您使用当前语言的特定术语,并在比较讨论中预先定义您的术语。与往常一样,你在演讲时选择的术语涉及到听众能够轻松理解你和听众可能陷入混乱之间的权衡:如果你正在谈论内存管理,可能会尽可能具体,但如果你正在松散地谈论,那么只需使用一些您的听众可以理解的词并准备好进行澄清。
最坏的情况是你的老师不清楚,我不知道这些术语的任何一个规范定义,但它们可能会给那些从其他语言学习过非常具体的定义的人带来困惑。
In almost all usual cases, Python does not have constructors in the same sense used by other OO languages because manually managing memory is generally discouraged. Instead, what you should usually do is define an __init__ method on the class. This method is called to initialize the new instance object automatically, first thing after it is constructed. Thus, it is not really a constructor, and talking about it as a constructor might confuse some people.
Of course some people want to call it a constructor because it is used a little bit like a constructor - fundamentally you can call it whatever you want as long as everyone understands what you are actually referring to. But in general, to be explicit and make yourself understood, call it an init method or something other than a constructor. Fundamentally, different languages just come with somewhat different terminology and speaking very clearly will always require adjustment to your subject matter and audience.
In Python it is possible to manage instance creation and destruction at a finer granularity, though you won't want to unless you know what you're doing. This is done by defining __new__ and __del__ methods to hook object instantiation and del statements. Whether these qualify as constructors and destructors precisely is a little more debatable (Python docs call the del method a destructor, but tend to be vaguer on what constitutes a constructor, e.g. including many functions which return object instances). I'd still encourage you to use the specific terminology for the language at hand, and in comparative discussions to define your terms up front. As always, your choice of terms while speaking involves tradeoffs between the audience being able to easily follow you and the audience potentially being led into confusion: if you are talking about memory management probably be as specific as possible, but if you are talking loosely then just use some word your audience understands and be ready to clarify.
Your instructor is being unclear at worst, I'm not aware of any one canonical definition of these terms but they might cause confusion for people who have learned very specific definitions from other languages.
http://docs.python.org/reference/datamodel.html#basic-customization
__new__
- 构造函数。__init__
- 初始化器。http://docs.python.org/reference/datamodel.html#basic-customization
__new__
- constructor.__init__
- initializer.