473,394 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

creating array of python objects

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.

Faheem.

Jul 18 '05 #1
4 3645
I don't believe that numarray supports arrays of Python objects.
The list of Numarray types doesn't list them:
http://stsdas.stsci.edu/numarray/num...ype-specifiers

This is the meaning of the "not a numeric type" message.

Using numpy, you can create an array of PyObjects where each entry is a
reference to the same cell object:
class cell: ... def setrow(self,row):
... self.row = row
... def setcol(self,col):
... self.col = col
... empcell = cell()
a = Numeric.reshape([empcell]*4, (2,2))
a[0][0].setrow(10)
a[1][1].row # because they are the same object 10
If you want distinct objects, you'll have to use a = Numeric.reshape([cell() for i in range(4)], (2,2))


Jeff

Jul 18 '05 #2


On Sun, 14 Mar 2004, Jeff Epler wrote:
I don't believe that numarray supports arrays of Python objects.
The list of Numarray types doesn't list them:
http://stsdas.stsci.edu/numarray/num...ype-specifiers

This is the meaning of the "not a numeric type" message.
Todd Miller said some stuff had not been documented yet. In any case, this
is from a numarray help page!
Using numpy, you can create an array of PyObjects where each entry is a
reference to the same cell object:
>>> class cell: ... def setrow(self,row):
... self.row = row
... def setcol(self,col):
... self.col = col
... >>> empcell = cell()
>>> a = Numeric.reshape([empcell]*4, (2,2))
>>> a[0][0].setrow(10)
>>> a[1][1].row # because they are the same object 10
If you want distinct objects, you'll have to use >>> a = Numeric.reshape([cell() for i in range(4)], (2,2))


Jeff


Thanks for the info. However, I was going by

numarray.objects

FILE
/usr/lib/python2.3/site-packages/numarray/objects.py

DESCRIPTION
objects -- This module defines a class and set of functions for
manipulating arrays of Python objects. It defines functions which
correspond to most of the most of the operators defined in Python's
operator module, and provides names compatible with most of numarray's
universal functions.
...

I thought this meant I *could* create an array of python objects. Am I
misunderstanding something?

Thanks for the help.
Faheem.

Jul 18 '05 #3

"Faheem Mitha" <fa****@email.unc.edu> wrote in message
news:sl*******************@Chrestomanci.home.earth ...
Dear People,

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


Given that the purpose of Numerical Python and NumArray are to make
available fast unboxed raw number array computations, I wonder whether a
plain old simple list of lists wouldn't serve your purpose, at least for
prototyping:

Cells = [[cell(),cell()], [cell(),cell()]]
# easily generalized with nested list comprehensions.

Terry J. Reedy

Jul 18 '05 #4
On Sun, 14 Mar 2004 13:37:36 -0600, Jeff Epler <je****@unpythonic.net> wrote:
I don't believe that numarray supports arrays of Python objects.
The list of Numarray types doesn't list them:
http://stsdas.stsci.edu/numarray/num...ype-specifiers

This is the meaning of the "not a numeric type" message.

Using numpy, you can create an array of PyObjects where each entry is a
reference to the same cell object:
>>> class cell: ... def setrow(self,row):
... self.row = row
... def setcol(self,col):
... self.col = col
... >>> empcell = cell()
>>> a = Numeric.reshape([empcell]*4, (2,2))
>>> a[0][0].setrow(10)
>>> a[1][1].row # because they are the same object 10
Is a[0,0] equivalent to a[0][0]? It is a slightly more compact representation.
If you want distinct objects, you'll have to use >>> a = Numeric.reshape([cell() for i in range(4)], (2,2))


Thanks. Faheem.
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Fuzzyman | last post by:
Pythons internal 'pointers' system is certainly causing me a few headaches..... When I want to copy the contents of a variable I find it impossible to know whether I've copied the contents *or*...
4
by: PhilC | last post by:
Hi Folks, If I have an array holding a pair of numbers, and that pairing is unique, is there a way that I can find the array index number for that pair? Thanks, PhilC
8
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res =...
5
by: Chris | last post by:
Hi, to create an array of 2 objects (e.g. of type '__gc class Airplane') I need to do : Airplane * arrAirplanes __gc = new Airplane* __gc; arrAirplanes = new Airplane("N12344"); arrAirplanes...
11
by: seberino | last post by:
Suppose a C extension locally built an array of PyObject* 's as follows... my_array = malloc(n * sizeof(PyObject*)); for (i = 0; i < n; i++) { my_array = PyList_New(0); } Q1: Must I do a...
10
by: Ruan | last post by:
My confusion comes from the following piece of code: memo = {1:1, 2:1} def fib_memo(n): global memo if not n in memo: memo = fib_memo(n-1) + fib_memo(n-2) return memo I used to think that...
1
by: iwl | last post by:
Hello, there is an builtin documented array module in phyton, but no documentation how such an array can be created in C-Extension functions. So I have to use Lists at time, but this is I think...
3
by: Bartholomew Simpson | last post by:
I am writing some C++ wrappers around some legacy C ones - more specifically, I am providing ctors, dtors and assignment operators for the C structs. I have a ton of existing C code that uses...
6
sammyboy78
by: sammyboy78 | last post by:
I'm trying to display my array of objects in a GUI. How do I get JLabel to refer to the data in my objects? I've read my textbook and some tutorials online I just cannot get this. Plus all the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.