properties + types, implementing meta-class desciptors elegantly?

Mike C. Fletcher mcfletch at rogers.com
Fri Jul 18 08:48:44 EDT 2003


Hi all,

I'm working on a base meta-type for a plug-in system, and I'd really 
like to use the same rich-descriptor objects as I've used everywhere 
else in the system.  Basically these are descriptors that intercept 
x.name, do various transformations, and then store the values in the 
instance dictionary.

Unfortunately:

 >>> type(t).pluginRole
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "p:\properties\basicproperty\basic.py", line 231, in __get__
    return self.getDefault( client )
  File "p:\properties\basicproperty\basic.py", line 256, in getDefault
    setattr( client, self.name, value )
  File "p:\properties\basicproperty\basic.py", line 283, in __set__
    self._setValue( client, value )
  File "p:\properties\basicproperty\basic.py", line 151, in _setValue
    client.__dict__[ self.name ] = value
TypeError: object does not support item assignment

which would seem to suggest that the only way to use the regular 
descriptors would be to do the (annoying) '_'+name thing so that there's 
a proliferation of names in the class (which I *really* don't want).  
So, does anyone have a pattern which allows setting an attribute on a 
class which doesn't go through the setattr machinery (i.e. can be used 
within a descriptor)? 

What I'm using right now (adding a "_PlugIn__properties" dictionary) 
feels a little hackish, (though it does work):

class PlugIn( type ):
    """Meta-class for plug-in classes"""
    def __new__( cls, name, bases, dictionary):
        dictionary['_PlugIn__properties'] = {}
        new = super( PlugIn, cls).__new__( cls, name, bases, dictionary)
        return new

especially as it requires that I sub-class each and every 
descriptor-type I want to use with plugins... not a huge deal, they're 
built to make that easy, but it seems inelegant to be creating an extra 
__dict__ just because the built-in one is marked as non-assignable save 
through the built-in setattr mechanism.

Basically, I'd love to find a method "simple_setattr" which allows the 
setting on the class w/out triggering the property I'm trying to implement.

Always-looking-for-elegance-even-when-it-already-works,
Mike

_______________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://members.rogers.com/mcfletch/








More information about the Python-list mailing list