[MATRIX-SIG] NumPy arrays of python objects

Timothy A. Hochberg Timothy A. Hochberg" <hochberg@wwa.com
Mon, 18 Aug 1997 09:40:02 -0500 (CDT)

On Mon, 18 Aug 1997, Michael Bell wrote:

> 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?

I've never used them "for real", but maybe the following will be enough to
get you started:

from Numeric import *

# Make some empty object arrays.
l1 = zeros((3,), PyObject)
l2 = zeros((3,), PyObject)

# Tha data we'll be using
d1 = ([1,2,3], ["a", 4, 5], [None, 0 , "spam"])
d2 = ([1,5,3], ["a", 6, 5], [None, 0 , "spam"])

# Objects are hard to initialize, so we'll use a loop.
# l1[:] = d1 doesn't work (try it and see why...)
for i in range(3):
    l1[i] = d1[i]
    l2[i] = d2[i]

print l1
print l2
# This should probably work but doesn't ...
##print equal(l1, l2)
##print less(l1, l2)
print l1 + l2
print argsort(l1)

> I'd really like to make an array of lists, but I can't see how to.
> I can get by with a list of list of lists, but it's clumsy.  Does the
> array of objects feature truly exist?

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.


|Tim Hochberg            Research Assistant      |
|hochberg <at> wwa.com   University of Illinois  |
|                        Electrical Engineering  |

MATRIX-SIG  - SIG on Matrix Math for Python

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