views:

58

answers:

2

I have a class with attributes which have a reference to another attribute of this class. See class Device, value1 and value2 holding a reference to interface:

class Interface(object):
    def __init__(self):
        self.port=None

class Value(object):
    def __init__(self, interface, name):
        self.interface=interface
        self.name=name

    def get(self):
        return "Getting Value \"%s\" with interface \"%s\""%(self.name, self.interface.port)

class Device(object):
    interface=Interface()
    value1=Value(interface, name="value1")
    value2=Value(interface, name="value2")

    def __init__(self, port):
        self.interface.port=port

if __name__=="__main__":
    d1=Device("Foo")
    print d1.value1.get() # >>> Getting Value "value1" with interface "Foo"
    d2=Device("Bar")
    print d2.value1.get() # >>> Getting Value "value1" with interface "Bar"
    print d1.value1.get() # >>> Getting Value "value1" with interface "Bar"

The last print is wrong, cause d1 should have the interface "Foo". I know whats going wrong: The line interface=Interface() line is executed, when the class definition is parsed (once). So every Device class has the same instance of interface.

I could change the Device class to:

class Device(object):
    interface=Interface()
    value1=Value(interface, name="value1")
    value2=Value(interface, name="value2")

    def __init__(self, port):
        self.interface=Interface()
        self.interface.port=port

So this is also not working: The values still have the reference to the original interface instance and the self.interface is just another instance...

The output now is:

>>> Getting Value "value1" with interface "None"
>>> Getting Value "value1" with interface "None"
>>> Getting Value "value1" with interface "None"

So how could I solve this the pythonic way? I could setup a function in the Device class to look for attributes with type Value and reassign them the new interface. Isn't this a common problem with a typical solution for it?

Thanks!

+2  A: 

interface is a class attribute. So when you do

d2=Device("Bar")

you are changing the port of the interface for all objects of class Device.


If you want to have these attributes per object instance, you have to put them into the __init__ method:

class Device(object):
    def __init__(self, port):
        self.interface=Interface()
        self.value1=Value(self.interface, name="value1")
        self.value2=Value(self.interface, name="value2")
        self.interface.port=port

You should only use class attributes if you really want to share a value with all the objects.

Other than that, I don't know what you try to accomplish here. Why defining it as class attribute in the first place?

Felix Kling
I thought about this, but now a class inspector don't see value1 and value2. This helps a lot for documentation and intellisense feature of editors. But seems a reasonable solution.
Guenther Jehle
@Guenther Jehle: You should not code for the editor (what happens if you change it, or someone else uses another one?), you should code in a way that is most logical for your application. Regarding documentation: Provide good doc strings.
Felix Kling
A: 

You can't defer the interface lookup unless your values are methods of Device. Perhaps something like this does what you want?

def value(name):
    def get(self):
        print 'Getting %s from port %s' % (name, self.interface.port)
    def set(self, v):
        print 'Setting %s to %s on port %s' % (name, v, self.interface.port)
    return property(get, set)

class Device(object):
    value1 = value('value1')
    value2 = value('value2')
    def __init__(self, interface):
        self.interface = interface

class Interface(object):
    def __init__(self, port):
        self.port = port

d = Device(Interface(1234))
d.value1
d.value2 = 42

If you want your values to do more than get and set, then it's more difficult.

Paul Hankin