python中的构造函数和初始化函数有什么区别? [英] What is the difference between a constructer and initializer in python?

查看:855
本文介绍了python中的构造函数和初始化函数有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Python(和Python C API): 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.

推荐答案

在大多数OO语言中,他们是一样的步骤,所以他对java,c ++等东西没有错。在python中,它们分两个步骤: __ 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中的构造函数和初始化函数有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆