Hey dischi,
Following up from our IRC conversation earlier, I've implemented a
subclass for property which provides functionality that will be in
Python 2.6:
http://bugs.python.org/issue1416
It allows setting all property accessors using decorators (rather than
just getters, which is what property currently does).
Inheritance is a bit cumbersome, but it makes sense when you understand
it. :) However, I think in the cases where inheritance really is
expected (like Callback._get_callback()) there is nothing wrong with
using a method.
So I recommend we add this property subclass to kaa.utils. When python
2.6 is being used, kaa.utils.property will just be the stock 2.6
property function, otherwise it will be the custom implementation.
See attached.
class property(property):
"""
Replaces built-in property function to extend it as per
http://bugs.python.org/issue1416
"""
def __init__(self, fget = None, fset = None, fdel = None, doc = None):
super(property, self).__init__(fget, fset, fdel)
self.__doc__ = doc or fget.__doc__
def _add_doc(self, prop, doc = None):
prop.__doc__ = doc or self.__doc__
return prop
def setter(self, fset):
if isinstance(fset, property):
# Wrapping another property, use deleter.
self, fset = fset, fset.fdel
return self._add_doc(property(self.fget, fset, self.fdel))
def deleter(self, fdel):
if isinstance(fdel, property):
# Wrapping another property, use setter.
self, fdel = fdel, fdel.fset
return self._add_doc(property(self.fget, self.fset, fdel))
def getter(self, fget):
return self._add_doc(property(fget, self.fset, self.fdel), fget.__doc__ or self.fget.__doc__)
class A(object):
_foo = 0
@property
def foo(self):
"""Properties can have docstrings."""
return self._foo
@foo.setter
def foo(self, value):
self._foo = value
class B(A):
@A.foo.getter
def foo(self):
# Overrides base class
return A.foo.fget(self) * 10
@foo.setter
@foo.deleter
def foo(self, value = 0):
A.foo.fset(self, value * 2)
a = A()
a.foo = 42
print a.foo
b = B()
b.foo = 10
print b.foo
del b.foo
print b.foo
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Freevo-devel mailing list
Freevo-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/freevo-devel