Python和C ++构造函数之间的差异 [英] Differences Between Python and C++ Constructors

查看:106
本文介绍了Python和C ++构造函数之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我一直在学习有关Python的更多信息,并且在阅读优秀的深入Python 时,作者指出此处 __ init __ 方法是



我有两个问题:



  1. C ++如何构造对象和
    Python如何构造对象之间有什么区别?



  2. 使构造函数成为构造函数的原因,
    以及 __ init __ 方法
    无法满足此条件?



解决方案

作者得出的区别是,就Python语言而言,您有一个指定类型之前的有效对象,甚至可以输入 __ init __ 。因此,它不是构造函数,因为在C ++中,从理论上讲,构造函数会将无效的,预先构造的对象转换为适当的类型的完成对象。



基本上,在Python中将 __ new __ 定义为返回新对象实例,而C ++新运算符仅返回一些内存,而该内存还不是任何类的实例。



但是,Python中的 __ init __ 可能是您首先建立一些重要的类不变式的地方(它具有什么属性,仅对于入门者而言) 。因此,就您的类的用户而言,它也可能是构造函数。只是Python运行时不关心这些不变量。如果您愿意,它对构成构造对象的标准很低。



我认为作者提出了一个正确的观点,这肯定是一个有趣的说法, Python创建对象。不过,这是一个很好的区别,而且我怀疑调用 __ init __ 构造函数是否会导致代码损坏。



另外,我注意到Python文档将 __ init __ 称为构造函数(http://docs.python.org/release/2.5.2/ref/customization.html)


作为对
构造函数的特殊约束,可能不会返回任何值


...因此,如果在考虑将 __ init __ 作为构造函数时遇到任何实际问题,那么Python就麻烦了! p>

Python和C ++构造对象的方式有些相似。两者都调用具有相对简单职责的函数(对于对象实例,调用 __ new __ ,对于某些版本的 operator new ),然后都调用一个函数,该函数有机会做更多工作以将对象初始化为有用的状态( __ init __ 与构造函数)。



实际的区别包括:




  • 在C ++中,基类的无参数构造函数会自动调用如果需要,可以按适当的顺序排列,而对于Python中的 __ init __ ,则必须使用自己的 __ init __ 显式初始化基址。即使在C ++中,也必须指定基类构造函数(如果它具有参数)。


  • 在C ++中,对于构造函数抛出异常会发生什么情况,您具有完整的机制在为已构造的子对象调用析构函数方面是一个例外。在Python中,我认为运行时(最多)会调用 __ del __




然后还有一个区别,就是 __ new __ 只是分配内存,它必须返回一个实际的对象实例。再说一次,原始内存并不是真正适用于Python代码的概念。


I've been learning more about Python recently, and as I was going through the excellent Dive into Python the author noted here that the __init__ method is not technically a constructor, even though it generally functions like one.

I have two questions:

  1. What are the differences between how C++ constructs an object and how Python "constructs" an object?

  2. What makes a constructor a constructor, and how does the __init__ method fail to meet this criteria?

解决方案

The distinction that the author draws is that, as far as the Python language is concerned, you have a valid object of the specified type before you even enter __init__. Therefore it's not a "constructor", since in C++ and theoretically, a constructor turns an invalid, pre-constructed object into a "proper" completed object of the type.

Basically __new__ in Python is defined to return "the new object instance", whereas C++ new operators just return some memory, which is not yet an instance of any class.

However, __init__ in Python is probably where you first establish some important class invariants (what attributes it has, just for starters). So as far as the users of your class are concerned, it might as well be a constructor. It's just that the Python runtime doesn't care about any of those invariants. If you like, it has very low standards for what constitutes a constructed object.

I think the author makes a fair point, and it's certainly an interesting remark on the way that Python creates objects. It's quite a fine distinction, though and I doubt that calling __init__ a constructor will ever result in broken code.

Also, I note that the Python documentation refers to __init__ as a constructor (http://docs.python.org/release/2.5.2/ref/customization.html)

As a special constraint on constructors, no value may be returned

... so if there are any practical problems with thinking of __init__ as a constructor, then Python is in trouble!

The way that Python and C++ construct objects have some similarities. Both call a function with a relatively simple responsibility (__new__ for an object instance vs some version of operator new for raw memory), then both call a function which has the opportunity to do more work to initialize the object into a useful state (__init__ vs a constructor).

Practical differences include:

  • in C++, no-arg constructors for base classes are called automatically in the appropriate order if necessary, whereas for __init__ in Python, you have to explicitly init your base in your own __init__. Even in C++, you have to specify the base class constructor if it has arguments.

  • in C++, you have a whole mechanism for what happens when a constructor throws an exception, in terms of calling destructors for sub-objects that have already been constructed. In Python I think the runtime (at most) calls __del__.

Then there's also the difference that __new__ doesn't just allocate memory, it has to return an actual object instance. Then again, raw memory isn't really a concept that applies to Python code.

这篇关于Python和C ++构造函数之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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