property and virtuality

Steven Bethard steven.bethard at gmail.com
Thu Mar 31 12:16:09 EST 2005


Laszlo Zsolt Nagy wrote:
> 
> My problem is about properties and the virtuality of the methods. I 
> would like to create a property whose get and set methods
> are virtual.

Perhaps you want to roll your own VirtualProperty descriptor?  Here's 
one based off the property implementation in Raymond Hettinger's How-To 
Guide for Descriptors[1]:

py> class VirtualProperty(object):
...     def __init__(self, getname=None, setname=None, delname=None,
...                  doc=None):
...         self.getname = getname
...         self.setname = setname
...         self.delname = delname
...         self.__doc__ = doc
...     def __get__(self, obj, type=None):
...         if obj is None:
...             return self
...         if self.getname is None:
...             raise AttributeError('unreadable attribute')
...         try:
...             fget = getattr(obj, self.getname)
...         except AttributeError:
...             raise TypeError('%s object does not have a %s method' %
...                             (type(obj).__name__, self.getname))
...         return fget()
...     def __set__(self, obj, value):
...         if self.setname is None:
...             raise AttributeError("can't set attribute")
...         try:
...             fset = getattr(obj, self.setname)
...         except AttributeError:
...             raise TypeError('%s object does not have a %s method' %
...                             (type(obj).__name__, self.setname))
...         fset(value)
...     def __delete__(self, obj):
...         if self.delname is None:
...             raise AttributeError("can't delete attribute")
...         try:
...             fdel = getattr(obj, self.delname)
...         except AttributeError:
...             raise TypeError('%s object does not have a %s method' %
...                             (type(obj).__name__, self.delname))
...         fdel()
...
py> class C(object):
...     def getx(self):
...         return 'C'
...     x = VirtualProperty('getx', 'setx', 'delx')
...
py> class D(C):
...     def getx(self):
...         try:
...             return self._x
...         except AttributeError:
...             return 'D'
...     def setx(self, x):
...         self._x = x
...
py> c = C()
py> c.x
'C'
py> c.x = 1
Traceback (most recent call last):
   File "<interactive input>", line 1, in ?
   File "<interactive input>", line 24, in __set__
TypeError: C object does not have a setx method
py> del c.x
Traceback (most recent call last):
   File "<interactive input>", line 1, in ?
   File "<interactive input>", line 33, in __delete__
TypeError: C object does not have a delx method
py> d = D()
py> d.x
'D'
py> d.x = 1
py> d.x
1
py> del d.x
Traceback (most recent call last):
   File "<interactive input>", line 1, in ?
   File "<interactive input>", line 33, in __delete__
TypeError: D object does not have a delx method

STeVe

[1] http://users.rcn.com/python/download/Descriptor.htm



More information about the Python-list mailing list