Subclassing numpy.ndarray - why is __array_finalize__ not being called twice here?
<p>According to <a href="http://docs.scipy.org/doc/numpy/user/basics.subclassing.html" rel="nofollow">this</a> primer on subclassing <code>ndarray</code>, the <code>__array_finalize__</code> method is guaranteed to be called, no matter if the subclass is instantiated directly, casted as a view or created from a template.</p>

<p>In particular, when calling the constructor explicitly, the order of methods called is <code>__new__</code> -> <code>__array_finalize__</code> -> <code>__init__</code>.</p>

<p>I have the following simple subclass of <code>ndarray</code> which allows an additional <code>title</code> attribute.</p>

<pre><code>class Frame(np.ndarray):    
    def __new__(cls, input_array, title='unnamed'):
        print 'calling Frame.__new__ with title {}'.format(title)
        self = input_array.view(Frame) # does not call __new__ or __init__
        print 'creation of self done, setting self.title...'
        self.title = title
        return self

    def __array_finalize__(self, viewed):
        # if viewed is None, the Frame instance is being created by an explicit
        # call to the constructor, hence Frame.__new__ has been called and the
        # title attribute is already set
        #
        # if viewed is not None, the frame is either being created by view
        # casting or from template, in which case the title of the viewed object
        # needs to be forwarded to the new instance
        print '''calling Frame.__array_finalize__ with type(self) == {} and
        type(viewed) == {}'''.format(type(self), type(viewed))

        if viewed is not None:
            self.title = getattr(viewed, 'title', 'unnamed')

        print self.title
</code></pre>

<p>which produces the following output:</p>

<pre><code>&gt;&gt;&gt; f = Frame(np.arange(3), 'hallo')
calling Frame.__new__ with title hallo
calling Frame.__array_finalize__ with type(self) == &lt;class '__main__.Frame'&gt; and
        type(viewed) == &lt;type 'numpy.ndarray'&gt;
unnamed
creation of self done, setting self.title...
&gt;&gt;&gt; f.title
'hallo'
</code></pre>

<p>As you can see, <code>__array_finalize__</code> is being called as a result of the line</p>

<pre><code> self = input_array.view(Frame)
</code></pre>

<p>Question: why is <code>__array_finalize__</code> not being called again as part of the <code>__new__</code> -> <code>__array_finalize__</code> -> <code>__init__</code> chain?</p>

<p>In the documentation you link to, it describes how <code>ndarray.__new__</code> will call <code>__array_finalize__</code> on the arrays it constructs. And your class's <code>__new__</code> method is causing that to happen when you create your instance as a <code>view</code> of an existing array. The <code>view</code> method on the array argument is calling <code>ndarray.__new__</code> for you, and it calls your <code>__array_finalize__</code> method before the instance is returned to you.</p>

<p>You don't see <code>__array_finalize__</code> called twice because you're not calling <code>ndarray.__new__</code> a second time. If your <code>__new__</code> method included a call to <code>super().__new__</code> in addition to the <code>view</code> call, you probably would see <code>__array_finalized__</code> called twice. Such behavior would probably be buggy (or at least, slower than necessary), so it's no surprise you're not doing it!</p>

<p>Python doesn't automatically call overridden methods when the overriding subclass's method gets called. It's up to the overriding method to call (or not call) the overridden version (directly with <code>super</code> or, as in this case, indirectly via another object's <code>view</code> method).</p>

python numpy inheritance