Numeric array of objects

Discussion in 'Python' started by beliavsky@aol.com, Mar 3, 2004.

  1. Guest

    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.
     
    , Mar 3, 2004
    #1
    1. Advertising

  2. Larry Bates Guest

    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


    <> wrote in message
    news:...
    > 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.
     
    Larry Bates, Mar 3, 2004
    #2
    1. Advertising

  3. > 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
     
    Josiah Carlson, Mar 4, 2004
    #3
  4. John Hunter Guest


    > 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
     
    John Hunter, Mar 4, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    946
    X-Centric
    Jun 30, 2005
  2. 7stud
    Replies:
    11
    Views:
    700
    Dennis Lee Bieber
    Mar 20, 2007
  3. darrel
    Replies:
    4
    Views:
    828
    darrel
    Jul 19, 2007
  4. jobs

    int to numeric numeric(18,2) ?

    jobs, Jul 21, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    971
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Jul 22, 2007
  5. Debajit  Adhikary

    Accessing Numeric Fields in Objects with Dot Notation

    Debajit Adhikary, Oct 8, 2007, in forum: Javascript
    Replies:
    2
    Views:
    97
    Thomas 'PointedEars' Lahn
    Oct 8, 2007
Loading...

Share This Page