properties + types, implementing meta-class desciptors elegantly?

Mike C. Fletcher mcfletch at rogers.com
Sun Jul 20 12:10:45 EDT 2003


Michele Simionato wrote:

>"Mike C. Fletcher" <mcfletch at rogers.com> wrote in message news:<mailman.1058629890.30475.python-list at python.org>...
><snip>
>
>As others before me, I am not sure of what you are trying to do
>and why you are doing so. I use descriptors in this way:
>
>class o(object):
>    class p( object ): # descriptor class
>        def __set__( self, client, value, *args, **named ):
>            print '__set__', self, client, value, args, named
>            self.v=value
>        def __get__(self,obj,cls):
>            return self.v
>    v = p()
>
You are aware that you have just created a class-attribute, rather than 
an instance attribute?  The property/descriptor object exists within the 
class 'o' namespace, not the instance namespace.  Try creating two 
instances of your o class and setting v on both of them.  There is one 
'p' instance "self" to which you are assigning/retrieving an attribute 
for all instances of o.  Creating class variables is certainly a valid 
use for descriptors, but I'm not sure if that's really what you were 
trying to do.

What I'm looking for, in terms of your code, is a method/function which 
would do the proper thing instead of using "self.v=value", (or rather 
client.__dict__['v'] = value (i.e. store the value in the client 
dictionary)) for all of the major built-in types, such as 
object-instances, classes (and object-instances with slots would be 
nice).  To the best of my knowledge, such a function does not exist 
within Python; for instances, simply doing instance.__dict__[ key ] = 
value is sufficient, but classes do not have a method exposed AFAIK 
which allows an equivalent setting of a value *without* triggering the 
descriptor machinery for the given key.

I'm creating a slightly more involved pattern, by the way:

class plugin( type ):
    someHelperClass = common.ClassByNameProperty(
       "someHelperClass", """Documentation for this meta-property""",
       defaultValue = "some.package.module.ClassName",
       setDefaultOnGet = 0,
    )

class MyWorkingPlugIn( myBaseImplementation ):
    __metaclass__ = plugin

instance = MyWorkingPlugIn()

Where the properties of the plugin meta-class are providing all sorts of 
services for the MyWorkingPlugIn class, such as allowing it to find 
"someHelperClass" related to the plug-in system while allowing that 
property to be set manually if desired, or automatically calculating a 
global identifier if the plug-in doesn't currently have a global 
identifier.  The value is primarily that the meta-class will use exactly 
the same mechanisms as the rest of the system, and so will be readily 
dealt with by the property-based meta-application system. 

There are certainly other ways to get around the particular problem (I 
have already done that), I'm looking for the *elegant* solution to the 
*general* case.

Enjoy,
Mike

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








More information about the Python-list mailing list