[Numpy-discussion] Array Protocol change for Python 2.6 (original) (raw)

Sasha ndarray at mac.com
Fri Jun 9 12:50:16 EDT 2006


On 6/9/06, Tim Hochberg <tim.hochberg at cox.net> wrote:

Sasha wrote: ... >> >My rule of thumb for choosing between an attribute and a method is >that attribute access should not create new objects. > Conceptually at least, couldn't there be a single arrayinterface object associated with a given array? In that sense, it doesn't really feel like creating a new object. In my view, conceptually, array_interface creates a adaptor to the array-like object. What are the advantages of it being an attribute? It is never settable, so the most common advantage of packing get/set methods in a single attribute can be rulled out. Saving typing of '()' cannot be taken seriousely when the name contains a pair of double underscores :-).

There was a similar issue discussed on the python-3000 mailing list with respect to hash method <http://mail.python.org/pipermail/python-3000/2006-April/000362.html>.

.... >> >My problem with arraystruct returning either a tuple or a CObject >is that array protocol sholuld really provide both. CObject is >useless for interoperability at python level and a tuple (or dict) is >inefficient at the C level. Thus a good array-like object should >really provide both arraystruct for use by C modules and >arraytuple (or whatever) for use by python modules. On the other >hand, making both required attributes/methods will put an extra burden >on package writers. Moreover, a pure python implementation of an >array-like object will not be able to provide arraystruct at all. > One possible solution would be an array protocol metaclass that adds >arraystruct to a class with arraytuple and arraytuple >to a class with arraystruct (yet another argument to make both >methods). > > I don't understand this. I'm don't see how bringing in metaclass is going to help a pure python type provide a sensible arraystruct. That seems like a hopeless task. Shouldn't pure python implementations just provide array?

My metaclass idea is very similar to your unpack_interface suggestion. A metaclass can autonatically add

def array_tuple(self): return unpack_interface(self.array_interface())

or

def array_interface(self): return pack_interface(self.array_tuple())

to a class that only implements only one of the two required methods.

A single attribute seems pretty appealing to me, I'm don't see much use for anything else.

I don't mind just having array_struct that must return a CObject. My main objection was against a method/attribute that may return either CObject or something else. That felt like shifting the burden from package writer to the package user.



More information about the NumPy-Discussion mailing list