#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    1
    Rep Power
    0

    Can I make a list of attribute references?


    Is it possible in Python to make a list of *references* to an objects attributes, and not just the values contained at the time? Basically, in java/c++ it would be like making a list of pointers?

    I've used Python a fair amount, but I've never wanted to do anything like this before...I'm wondering if it's even possible, or if I'm going about it the wrong way.

    more concretely, i want to be able to do something like the following:

    class foo:
    attributes=[]
    def __init__(self):
    self.x=0
    self.y=0
    self.attributes=[x,y]
    def change(self):
    self.x=1
    self.y=1

    where you would get the following results

    f=foo()
    print f.attributes
    >>[0,0]
    f.change()
    print f.attributes
    >>[1,1]
    f.x=2
    print f.attributes
    >>[2,1]

    What i'm doing doesnt work, so if you have any suggestions, I'd appreciate it.

    Thanks!
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Using new style classes you can do something like this:
    Code:
    class foo(object): 
        attributes = {'x': 0, 'y': 0}
    
        def setx(self, value): 
            self.attributes['x'] = value
        def getx(self): 
            return self.attributes['x']
        
        def sety(self, value): 
            self.attributes['y'] = value
        def gety(self): 
            return self.attributes['y']
    
        x = property(getx, setx, None, None)
        y = property(gety, sety, None, None)
    
    afoo = foo()
    print afoo.attributes
    afoo.x = 3.141
    afoo.y = "PI"
    print afoo.attributes
    print afoo.x, afoo.y
    Of course attributes could be a list if you wanted but a dictionary seems more useful.
    grim

IMN logo majestic logo threadwatch logo seochat tools logo