Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Numeric array of objects

Reply
Thread Tools

Numeric array of objects

 
 
beliavsky@aol.com
Guest
Posts: n/a
 
      03-03-2004
You create a 1-D Numeric array of n floats with

x = zeros(n,Float)

How do you create a Numeric array of n instances of class 'xy', where
for example xy is defined as follows:

class xy:
def __init__(self,x=0.0,y=0.0):
self.x = x
self.y = y

I want the elements of the array to be initialized to the default
value of xy, (0.0,0.0). I have read the Martelli's explanation on p309
of the book "Python in a Nutshell", but I still don't get it.
 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      03-03-2004
What you want is n "instances" of the class xy.
Since __init__ method is called when an instance
is created, the instance.x and instance.y
attributes will be 0.0, 0.0 unless you set them
to something different (as arguments to the
instance creation).

class xy:
def __init__(self,x=0.0,y=0.0):
self.x = x
self.y = y

n=10
x=[xy() for j in range(10)]

after that

x[0].x equals 0.0
x[0].y equals 0.0
..
..
..
x[9].x equals 0.0
x[9].y equals 0.0

I "think" this is what you want, but I'm not certain.

-Larry


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> You create a 1-D Numeric array of n floats with
>
> x = zeros(n,Float)
>
> How do you create a Numeric array of n instances of class 'xy', where
> for example xy is defined as follows:
>
> class xy:
> def __init__(self,x=0.0,y=0.0):
> self.x = x
> self.y = y
>
> I want the elements of the array to be initialized to the default
> value of xy, (0.0,0.0). I have read the Martelli's explanation on p309
> of the book "Python in a Nutshell", but I still don't get it.



 
Reply With Quote
 
 
 
 
Josiah Carlson
Guest
Posts: n/a
 
      03-04-2004
> You create a 1-D Numeric array of n floats with
>
> x = zeros(n,Float)
>
> How do you create a Numeric array of n instances of class 'xy', where
> for example xy is defined as follows:
>
> class xy:
> def __init__(self,x=0.0,y=0.0):
> self.x = x
> self.y = y
>
> I want the elements of the array to be initialized to the default
> value of xy, (0.0,0.0). I have read the Martelli's explanation on p309
> of the book "Python in a Nutshell", but I still don't get it.



Numeric.array([xy() for i in xrange(n)], Numeric.PyObject)


- Josiah
 
Reply With Quote
 
John Hunter
Guest
Posts: n/a
 
      03-04-2004

> How do you create a Numeric array of n instances of class 'xy', where
> for example xy is defined as follows:


from Numeric import PyObject, array

class xy:
def __init__(self,x=0.0,y=0.0):
self.x = x
self.y = y

a = array([xy() for i in range(10)], typecode=PyObject)

JDH

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing Numeric Fields in Objects with Dot Notation Debajit Adhikary Javascript 2 10-08-2007 12:55 PM
int to numeric numeric(18,2) ? jobs ASP .Net 2 07-22-2007 12:32 AM
Arithmetic overflow error converting numeric to data type numeric. darrel ASP .Net 4 07-19-2007 09:57 PM
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
check if string contains numeric, and check string length of numeric value ief@specialfruit.be C++ 5 06-30-2005 01:08 PM



Advertisments