[Numpy-discussion] numarraycore

Todd Miller jmiller at stsci.edu
Thu May 6 11:09:07 CDT 2004


On Wed, 2004-05-05 at 18:59, Colin J. Williams wrote:
> 
> Todd Miller wrote:
> > On Wed, 2004-05-05 at 16:27, Colin J. Williams wrote:
> >   
> > > It would help to have some documentation on the purpose and usage of the 
> > > class UsesOpPriority and its variable op_priority.
> > >     
> > 
> Todd,
> 
> Thanks for this, perhaps it could be added to the pdf .  I like the
> basic idea.

OK.  What is it you want to do?

> > The basic idea was that NumArray subclasses which want to use numarray
> > operators would:
> > 
> > 1.  subclass from UsesOpPriority 
> UsesOpPriority has no methods and thus, to access the methods of
> NumArray, it would appear to be necessary to subclass from NumArray.

For limited but important uses (e.g. masked array) subclassing from
NumArray is not necessary;  I'd summarize these as the cases where a
class wants to take control of operators from NumArray.

> > 2.  set a class level op_priority > 0. 
> > 
> > 
> > NumArrays have op_priority 0,  higher priorities are given "precedence".
> > 
> > 
> > Thus, given A=NumArray(...) and B=NumArraySubclass(...), and
> > A.op_priority==0 and B.op_priority==1, then:  
> > A+B would execute as B.__radd__(A) rather than A.__add__(B) and hence
> > the type(A+B) could be NumArraySubclass rather than NumArray.  Different
> > subclasses could use higher or lower op_priorities to perform the same
> > kind of operator resolution among themselves.
> >   
> I wonder about the desirability of having different levels of priority
> for the same level of subclassing and am puzzled that a float variable
> is used for the priority.

Different levels of priority for the same level of sub-classing makes
sense to me:  it let's you answer the question "What's the result type
of a PyMatrix instance + a SomeOtherSubclass instance?"

A float enables insertions of new members into the middle of an existing
order.

> 
> It would nice if this depth could be determined automatically. 
> Perhaps something like the script below could be used.

I don't think we want something automatic here.   The choices involved
are arbitrary so we just need to be able to define what we want.

Other than masked arrays, the problem we were trying to solve with
UsesOpPriority is more like:

class A(NumArray):              
	op_priority = 1

class B(NumArray):              
	op_priority = 2

whenever A op B is encountered, the result should be a B.  

That said, the solution we currently have is better described by:
whenever A op B is encountered, B.__rop__(A) is called in preference to
A.__op__(B).  With the example above,  even though B.__rop__ is called,
the result is still an A.  :-(   

I fixed this in CVS today to work more like the original description.

Regards,
Todd

> 
> Colin W.
> > Todd
> >   
> # tClassing.py
> 
> class A:
>   def __init__(self):
>     self.p= 0
>   def prior(self):
>     self.p+= 1
> class B(A):
>   def __init__(self):
>     A.__init__(self)
>     self.prior()
> class C(B):
>   def __init__(self):
>     B.__init__(self)
>     self.prior()
> 
> print A().p
> print B().p
> c= C()
> print 'Priority for C methods:', c.p
> ------------------------------------------------------- This SF.Net
> email is sponsored by Sleepycat Software Learn developer strategies
> Cisco, Motorola, Ericsson & Lucent use to deliver higher performing
> products faster, at low TCO.
> http://www.sleepycat.com/telcomwpreg.php?From=osdnemail3
> _______________________________________________ Numpy-discussion
> mailing list Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion
-- 
Todd Miller <jmiller at stsci.edu>





More information about the Numpy-discussion mailing list