Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is a difference between a constructor in C++ and an <code>__init__</code> method in Python. In C++, the task of a constructor is to construct an object. If it fails, no destructor is called. Therefore if any resources were acquired before an exception was thrown, the cleanup should be done before exiting the constructor. Thus, some prefer two-phase construction with most of the construction done outside the constructor (ugh).</p> <p>Python has a much cleaner two-phase construction (construct, then initialize). However, many people confuse an <code>__init__</code> method (initializer) with a constructor. The actual constructor in Python is called <code>__new__</code>. Unlike in C++, it does not take an instance, but returns one. The task of <code>__init__</code> is to initialize the created instance. If an exception is raised in <code>__init__</code>, the destructor <code>__del__</code> (if any) will be called as expected, because the object was already created (even though it was not properly initialized) by the time <code>__init__</code> was called.</p> <p>Answering your question:</p> <blockquote> <p>In Python, if code in your "constructor" fails, the object ends up not being defined.</p> </blockquote> <p>That's not precisely true. If <code>__init__</code> raises an exception, the object is created but not initialized properly (e.g., some attributes are not assigned). But at the time that it's raised, you probably don't have any references to this object, so the fact that the attributes are not assigned doesn't matter. Only the destructor (if any) needs to check whether the attributes actually exist.</p> <blockquote> <p>Whats a proper way of doing this?</p> </blockquote> <p>In Python, initialize objects in <code>__init__</code> and don't worry about exceptions. In C++, use <a href="http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization" rel="noreferrer">RAII</a>.</p> <hr> <p><strong>Update</strong> [about resource management]:</p> <p>In garbage collected languages, if you are dealing with resources, especially limited ones such as database connections, it's better not to release them in the destructor. This is because objects are destroyed in a non-deterministic way, and if you happen to have a loop of references (which is not always easy to tell), and at least one of the objects in the loop has a destructor defined, they will never be destroyed. Garbage collected languages have other means of dealing with resources. In Python, it's a <a href="http://www.python.org/dev/peps/pep-0343/" rel="noreferrer">with statement</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload