properties + types, implementing meta-class desciptors elegantly?

Michele Simionato mis6 at pitt.edu
Mon Jul 21 09:33:12 EDT 2003


"Mike C. Fletcher" <mcfletch at rogers.com> wrote in message news:<mailman.1058717611.28555.python-list at python.org>...
> 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.

I have just copied your code, I thought you wanted a class attribute.
It is is trivial to do the same for instance attributes:

class o(object):
    class p( object ): # descriptor class
        def __set__( self, client, value, *args, **named ):
            print '__set__', self, client, value, args, named
            self.value=value
        def __get__(self,obj,cls):
            return self.value

    def __init__(self):
        self.v = self.p()

I understand that you aware of this, but you don't like it. Still I
do not understand why do you feel it to be ugly and/or inelegant. It
seems to me quite idiomatic.

> 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.

Yes, I understand you want to be able to set class dictionaries just
as object dictionaries, bypassing descriptors. Still, I am not sure
if this would be a good idea.
 
> 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.
> 

Elegance is in the eye of the beholder ;)

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

Good luck with your project,

Michele




More information about the Python-list mailing list