[CentralOH] Delegate Property
Mark Erbaugh
mark at microenh.com
Sat Oct 9 15:22:27 CEST 2010
On Oct 9, 2010, at 12:38 AM, Mark Erbaugh wrote:
> 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
>
> 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
>
>
> 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.
I've come up with at least one solution that seems to work:
class B(object):
def __init__(self):
self.w = A()
def a(): #@NoSelf
fget = lambda self : self.w.a
fset = lambda self,v : self.w.a.__set__(v)
return (fget, fset)
a = property(*a())
Mark
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/mailman/private/centraloh/attachments/20101009/ff84f7b8/attachment.html>
More information about the CentralOH
mailing list