creating array of python objects

Faheem Mitha faheem at
Sun Mar 14 18:12:55 CET 2004

Dear People,

I have the current modest goal. Given a user defined class, I want to
creata an array of instances of that class.

Consider the following class.

class cell:
    def setrow(self,row):
        self.row = row
    def setcol(self,col):
        self.col = col

I create an "empty" cell.

empcell = cell()

Now I want to create an array of objects identical to empcell, and
later initialize them.

I tried stuff along the lines of 

import string
from numarray import *

empcell = cell()

base = [empcell]

mat = fromlist(base,shape=(2,2))

But the 

mat = fromlist(base,shape=(2,2))

line fails with

AttributeError: cell instance has no attribute '__len__'

Similar things fail with the same error message. No doubt I'm doing
something stupid, but would someone please enlighten me?

I got the fromlist command from 

>>> help("numarray.objects")

I was a bit disconcerted to find on trying to run the first example
from the help page that I got

>>> a = fromlist(["t","u","w"])
TypeError: Expecting a python numeric type, got something else.

What am I doing wrong?

I'm running

ii  python                    2.3.3-5                   An interactive
high-level object-oriented language (default versio
ii  python-numarray           0.8.1-1                   An array
processing package modelled after Python-Numeric
ii  python-numeric            23.1-1                    Numerical
(matrix-oriented) Mathematics for Python

on Debian sarge.

Thanks in advance and sorry for the long-winded message.


More information about the Python-list mailing list