Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://docs.python.org/2/reference/datamodel.html#object.__init__" rel="nofollow noreferrer"><code>__init__</code></a> is the hook used to initialize your instance. (it is always called when you create an instance).</p> <p><code>init__</code> is just a class method with a wonky name.</p> <p>You need to show us your code; if something is broken when you have a method named <code>__init__</code> you made a mistake there. Renaming it to <code>init__</code> just means it won't be called automatically, thus not triggering your coding mistake.</p> <p>In the <a href="https://stackoverflow.com/questions/11899959/python-is-it-possible-to-instantiate-an-object-of-one-class-in-two-different-wa#comment15841610_11900405">comment you refer to</a>, the author <em>did</em> use <code>__init__</code> in his comment but forgot to escape the leading underscores, and they were interpreted as code to start bold text instead:</p> <pre><code>__this is bold__ </code></pre> <p>becomes <strong>this is bold</strong>. Note that the trailing <code>__</code> on <code>__main__</code> <em>also</em> is lost in that comment.</p> <p>In your updated code, you are trying to override the <code>__init__</code> method of a (subclass) of tuple, which is a special case. By renaming the <code>__init__</code> method to <code>init__</code> you created a <em>different</em> method and did not run into this common problem at all.</p> <p>See <a href="https://stackoverflow.com/questions/1565374/subclassing-python-tuple-with-multiple-init-arguments">Subclassing Python tuple with multiple __init__ arguments</a> for more detail on why this is a problem; you have to create a <code>__new__</code> method instead. <a href="http://docs.python.org/2/reference/datamodel.html#object.__new__" rel="nofollow noreferrer"><code>__new__</code></a> is the factory method to create instances, <code>__init__</code> then initializes the data. But that doesn't work on immutable types such as <code>namedtuple</code> or <code>tuple</code>, since you are not supposed to change the instance after the factory created it.</p> <p>In this specific case, you do not need an <code>__init__</code> or a <code>__new__</code> method <em>at all</em> because the <code>namedtuple</code> subclass <code>Point</code> already takes care of the initialization of the <code>x</code>, <code>y</code> and <code>z</code> attributes. By renaming <code>__init__</code> to <code>init__</code> you made things work, but you do end up with a pointless <code>init__</code> method that you'll never use. Just delete it.</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. 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