[Numpy-discussion] numarray unfriendly to user defined types

Todd Miller jmiller at stsci.edu
Wed Sep 24 16:01:05 EDT 2003


On Wed, 2003-09-24 at 18:27, Tim Hochberg wrote:
> Hi Todd,
> 
> There are three ways to spell "defer to me" on the table (the precise 
> details of
> each spelling are, of course, still open for debate):
> 
>    1.  numarray.defer_to(my_class)
> 
>    2. class ArrayLike(numarray.DeferTo):
>          # ...
> 
>    3. class ArrayLike:
>          _numarray_defer_to = True
>         # ...
> 
> I'd prefer a non-registration solution since those are both 
> aesthetically displeasing and leave you open to the situation where a 
> class in module A gets registered by module B, but module C expects it 
> not to be registered and everything breaks. Not all that likely, I 
> admit, but let's avoid the registration version if we can.

I was picturing this as module A registering it's own classes only. 
Nevertheless, inverting the problem and distributing the registration as
you suggested is better.

> 
> The other two solutions are almost equivalent. The one case where 3 has 
> an edge over 2 is if I have an object (not a class), I could potentially 
> set a _numarray_defer_to on the object before passing it to numarray 
> without having to mess with the class
> of the object at all. YAGNI, though.

I was more concerned about the potential impact of lots of multiple
inheritance, but that's probably just my own personal blend of FUD.

> The advantage of 2 in my view is that it *does* force you to subclass. 
> With 3, there will be the temptation to poke into some other  module and 
> set _numarray_defer_to on some poor unsuspecting class. This has the 
> same disadvantage as 1, that it could confuse some other poor 
> unsuspecting module. The correct way to do get a deferred class from a 
> third party module is to import and subclass. This works with either 2 
> or 3::
> 
>   import A
> 
>   class Klass2(a.Klass, numarray.DeferTo):   #2
>      #...
> 
>   class Klass3(a.Klass): #3 the good way
>       _numarray_defer_to = True
>      # ...
> 
>    A.Klass._numarray_defer_to = True #3 the evil way.
> 
>  Version 2 is cleaner and encourages you to do the right thing, so I'd 
> prefer that solution.
> 

Good enough for me.  If no one else has any comments, then
numarray.DeferTo is where I'll start implementing.  Tomorrow.

> 
> regards,
> 
> -tim

Thanks again,
Todd








More information about the NumPy-Discussion mailing list