[CentralOH] Delegate Property

Mark Erbaugh mark at microenh.com
Sat Oct 9 06:38:39 CEST 2010


Is there a way to delegate a read/write property in a class instance to a read/write property in a different class's instance that is a data member of the class? The only way I can think of is to assign the property getter and setter, but with some ways of creating properties, getters and setters aren't easily gotten.

i.e.

class W(object):
    def a(): #@NoSelf
        doc = """Docstring""" #@UnusedVariable
       
        def fget(self):
            return self._a
           
        def fset(self, value):
            self._a = value
           
        def fdel(self):
            del self._a
           
        return locals()
       
    a = property(**a())

    def __init__(self):
        self._a = 10
        

class B(object):
    
    def __init__(self):
        self.w = W()

        
I would like to have an 'a' property on B such that when read they read the a from the contained self.w and when written they write that a.  In this example, the property is just using a local variable, but in a real-world case, the property could be doing some computations.

Thanks,
Mark
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/mailman/private/centraloh/attachments/20101009/a75d4db8/attachment.html>


More information about the CentralOH mailing list