[Numpy-discussion] __array_wrap__

Robert Kern robert.kern@gmail....
Tue Sep 29 14:12:35 CDT 2009


On Tue, Sep 29, 2009 at 14:08, Charles R Harris
<charlesr.harris@gmail.com> wrote:
>
>
> On Tue, Sep 29, 2009 at 12:48 PM, Robert Kern <robert.kern@gmail.com> wrote:
>>
>> On Tue, Sep 29, 2009 at 13:35, Charles R Harris
>> <charlesr.harris@gmail.com> wrote:
>> >
>> > On Tue, Sep 29, 2009 at 12:23 PM, Robert Kern <robert.kern@gmail.com>
>> > wrote:
>> >>
>> >> On Tue, Sep 29, 2009 at 13:09, Charles R Harris
>> >> <charlesr.harris@gmail.com> wrote:
>> >> >
>> >> >
>> >> > On Tue, Sep 29, 2009 at 11:00 AM, Neal Becker <ndbecker2@gmail.com>
>> >> > wrote:
>> >> >>
>> >> >> fixed_pt arrays need to apply the overflow_policy after operations
>> >> >> (overflow_policy could be clip, or throw exception).
>> >> >>
>> >> >> I thought __array_wrap__ would work for this, but it seems to not be
>> >> >> called
>> >> >> when I need it.  For example:
>> >> >>
>> >> >> In [13]: obj
>> >> >> Out[13]: fixed_pt_array([  0,  32,  64,  96, 128])
>> >> >>
>> >> >> In [14]: obj*100 < this should overflow
>> >> >> enter: [  0  32  64  96 128] << on entry into __array_wrap
>> >> >> enter: [0 32 64 96 128]
>> >> >> exit: [  0  32  64  96 128]
>> >> >> Out[14]: fixed_pt_array([    0,  3200,  6400,  9600, 12800])
>> >> >>
>> >> >> Apparantly, obj*100 is never passed to array_wrap.
>> >> >>
>> >> >> Is there another way I can do this?
>> >> >>
>> >> > I believe array wrap has to be explicitly called after the fact.
>> >>
>> >> Ufuncs call __array_wrap__ implicitly.
>> >>
>> >
>> > Thanks for the info. How do they decide which one to call?
>>
>> The .__array_priority__ attribute.
>
> What if they are the same?

http://docs.scipy.org/doc/numpy/user/c-info.beyond-basics.html?highlight=__array_priority__#ndarray.__array_priority__

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco


More information about the NumPy-Discussion mailing list