Re: Property in derived class
On May 9, 9:05 pm, George Sakkis <[EMAIL PROTECTED]> wrote: > Using the overridable property recipe [1], > [1]http://infinitesque.net/articles/2005/enhancing%20Python's%20property... Thanks, this is a great solution! Joseph -- http://mail.python.org/mailman/listinfo/python-list
Re: Property in derived class
On May 9, 5:20 pm, Joseph Turian <[EMAIL PROTECTED]> wrote: > If I have a property in a derived class, it is difficult to override > the get and set functions: the property's function object had early > binding, whereas the overriden method was bound late. > This was previously discussed: > http://groups.google.com/group/comp.lang.python/browse_thread/thread/... > > Could someone demonstrate how to implement the proposed solutions that > allow the property to be declared in the abstract base class, and > refer to a get function which is only implemented in derived classes? Using the overridable property recipe [1], it can be written as: class AbstractFoo(object): def _getFoo(self): raise NotImplementedError('Abstract method') def _setFoo(self, signals): raise NotImplementedError('Abstract method') foo = OProperty(_getFoo, _setFoo) HTH, George [1] http://infinitesque.net/articles/2005/enhancing%20Python's%20property.xhtml -- http://mail.python.org/mailman/listinfo/python-list
Re: Property in derived class
On Fri, May 9, 2008 at 3:20 PM, Joseph Turian <[EMAIL PROTECTED]> wrote: > Could someone demonstrate how to implement the proposed solutions that > allow the property to be declared in the abstract base class, and > refer to a get function which is only implemented in derived classes? One way is to have the property refer to a proxy that performs the late binding, which might look something like this: def _bar(self): return self.bar() prop = property(fget=_bar) Another way is to declare properties using something like the following indirectproperty class. I haven't thoroughly tested this, so I don't know whether it works exactly right. class indirectproperty(object): def __init__(self, sget=None, sset=None, sdel=None): self.sget = sget self.sset = sset self.sdel = sdel def __get__(self, instance, owner): if instance is not None: fget = getattr(instance, self.sget) else: fget = getattr(owner, self.sget) return fget() def __set__(self, instance, value): fset = getattr(instance, self.sset) fset(value) def __delete__(self, instance): fdel = getattr(instance, self.sdel) fdel() class Foo(object): def func(self): return "foo" callfunc = indirectproperty(sget="func") class Bar(Foo): def func(self): return "bar" -- http://mail.python.org/mailman/listinfo/python-list
Re: Property in derived class
Joseph Turian wrote: If I have a property in a derived class, it is difficult to override the get and set functions: the property's function object had early binding, whereas the overriden method was bound late. This was previously discussed: http://groups.google.com/group/comp.lang.python/browse_thread/thread/e13a1bd46b858dc8/9d32049aad12e1c1?lnk=gst#9d32049aad12e1c1 Could someone demonstrate how to implement the proposed solutions that allow the property to be declared in the abstract base class, and refer to a get function which is only implemented in derived classes? Thanks, Joseph Sounds a little like you are trying to write Java in Python. 1) You don't need get/set functions to get/set properties in Python. You just get the property directly using dotted notation. 2) Properties defined in base class exist in derived class unless you override them. class foobase(object): def __init__(self): self.myProperty = 1 class foo(foobase): self.__init__(self, myProperty=None) foobase.__init__(self) if myProperty is not None: self.myProperty = myProperty obj = foo() print obj.myProperty >>> 1 obj = foo(6) print obj.myProperty >>> 6 obj.myProperty = 19 print obj.myProperty >>> 10 I hope this was what you were looking for. If not, I don't understand the question. -Larry -- http://mail.python.org/mailman/listinfo/python-list
Property in derived class
If I have a property in a derived class, it is difficult to override the get and set functions: the property's function object had early binding, whereas the overriden method was bound late. This was previously discussed: http://groups.google.com/group/comp.lang.python/browse_thread/thread/e13a1bd46b858dc8/9d32049aad12e1c1?lnk=gst#9d32049aad12e1c1 Could someone demonstrate how to implement the proposed solutions that allow the property to be declared in the abstract base class, and refer to a get function which is only implemented in derived classes? Thanks, Joseph -- http://mail.python.org/mailman/listinfo/python-list