[MATRIX-SIG] NumPy arrays of python objects

Konrad Hinsen hinsen@ibs.ibs.fr
Mon, 18 Aug 1997 19:27:45 +0200

> The NumPy doc.html, and the Numeric Tutorial, both mention arrays of 
> Python objects, but with little elaboration.
> Can someone point me to, or send my some, sample code making use of
> these beasts?

Tim has done that, so I'll just add one comment: it is pretty easy
to create arrays of anything using the standard array constructor,
*except* for arrays of sequence objects (e.g. arrays of lists).
The reason is simply that the array constructor assumes that any
level of sequence object in its input should become a dimension of
the array.

That means that the only way to have an array of lists is to generate
first an appropriate array of "general objects" with whatever elements
you like, and then replace the elements by the lists using indexed

I don't think there's a way out - you can't have one syntax stand
for two meanings!

Commenting Tim's message:

> It exists - but it appears that some of the functionality that is
> available for numeric types is missing for PyObjects. Oddly 'abs' works
> which seems like it would be of limited utility for PyObjects, while
> 'less', which would be more useful, does not. I suspect some of the
> PyObject utility is missing because no one has really needed it, but maybe
> there is another reason.

I strongly suspect the reason is ease of implementation! ;-)

"less" is implemented as a ufunc in module umath, just like all the
mathematical functions. The ufunc's are optimized functions for
fast array operations, but they work only on the standard numerical
array types, not on general object arrays.

"abs", on the other hand, is a built-in Python function that can
easily be made to work for any type. When you apply abs() to a general
object array, it is simply mapped to the elements.

So which functions *are* available on general object arrays? All those
for which Python provides an extension interface, i.e. the basic
arithmetic functions plus some oddities like abs. And of course all
structural array functions that don't care about the values of the

As you may guess, I have used general object arrays in real life; they
work well and are quite useful, although only in rather special
circumstances. For example, they are very nice for passing data to
C routines.

Konrad Hinsen                          | E-Mail: hinsen@ibs.ibs.fr
Laboratoire de Dynamique Moleculaire   | Tel.: +33-
Institut de Biologie Structurale       | Fax:  +33-
41, av. des Martyrs                    | Deutsch/Esperanto/English/
38027 Grenoble Cedex 1, France         | Nederlands/Francais

MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org