python references

Discussion in 'Python' started by dustin.getz@gmail.com, Feb 5, 2007.

  1. Guest

    >>> from Numeric import zeros
    >>> p=zeros(3)
    >>> p

    array([0,0,0])
    >>> p[0]

    0
    >>> x=p[0]
    >>> x=10
    >>> p

    array([0,0,0]) #actual behavior
    #array([10,0,0]) #desired behavior

    I want x to be a C++-esque reference to p[0] for convenience in a
    vector3 class. i dont want accessor methods. i know python can do
    this, but it's been a long time since I used it and am unsuccessful in
    my googling and docreading. a little help please?
    , Feb 5, 2007
    #1
    1. Advertising

  2. wrote:

    >>>> from Numeric import zeros
    >>>> p=zeros(3)
    >>>> p

    > array([0,0,0])
    >>>> p[0]

    > 0
    >>>> x=p[0]
    >>>> x=10
    >>>> p

    > array([0,0,0]) #actual behavior
    > #array([10,0,0]) #desired behavior
    >
    > I want x to be a C++-esque reference to p[0] for convenience in a
    > vector3 class. i dont want accessor methods. i know python can do
    > this, but it's been a long time since I used it and am unsuccessful in
    > my googling and docreading. a little help please?


    Nope, python can't do this.

    Diez
    Diez B. Roggisch, Feb 5, 2007
    #2
    1. Advertising

  3. Rob Wolfe Guest

    wrote:
    > >>> from Numeric import zeros
    > >>> p=zeros(3)
    > >>> p

    > array([0,0,0])
    > >>> p[0]

    > 0
    > >>> x=p[0]


    `x' is now a reference to immutable integer object
    with value 0, not to first element of array `p'

    > >>> x=10


    now `x' is a reference to immutable integer object
    with value 10, array doesn't change

    > >>> p

    > array([0,0,0]) #actual behavior
    > #array([10,0,0]) #desired behavior
    >
    > I want x to be a C++-esque reference to p[0] for convenience in a
    > vector3 class. i dont want accessor methods. i know python can do
    > this, but it's been a long time since I used it and am unsuccessful in
    > my googling and docreading. a little help please?


    You can have such a reference to mutable objects.
    Consider this:

    >>> p = [[0,0,0], [0,0,0]]
    >>> x = p[0] # reference to mutable list object
    >>> x[0] = 10
    >>> p

    [[10, 0, 0], [0, 0, 0]]

    --
    HTH,
    Rob
    Rob Wolfe, Feb 5, 2007
    #3
    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. Roger Leigh
    Replies:
    8
    Views:
    408
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Replies:
    3
    Views:
    425
    Victor Bazarov
    Nov 10, 2004
  3. DanielEKFA
    Replies:
    8
    Views:
    581
    DanielEKFA
    May 16, 2005
  4. Replies:
    8
    Views:
    688
    Bruno Desthuilliers
    Dec 12, 2006
  5. Lars Willich
    Replies:
    13
    Views:
    816
    Ian Shef
    Oct 23, 2007
Loading...

Share This Page