properties + types, implementing meta-class desciptors elegantly?
Beni Cherniavsky
cben at techunix.technion.ac.il
Sun Jul 20 05:20:54 EDT 2003
Mike C. Fletcher wrote on 2003-07-19:
> * finally, stores the value
> o tries to do what would have been done if there were no
> descriptor (with the new, coerced value)
> o does *not* create new names in the object's namespace (all
> names are documented w/ descriptors, there's not a lot of
> '_' prefixed names cluttering the namespace)
> o does *not* require a new dictionary/storage-object attribute
> for the object (the descriptor works like any other
> descriptor, a *stand-alone* object that replaces a regular
> attribute)
>
> It's that last part that I'd like to have a function/method to
> accomplish. That is, store (and obviously retrieve as well) attribute
> values for objects, instances, types, classes, __slot__'d instances,
> etceteras under a given name without triggering any of the __setattr__
> machinery which defers to the __set__ method of the associated descriptor.
>
> I can work around the problem by violating either of those two bullet
> points under "finally, stores the value", but I'm looking for something
> that *doesn't* violate them. See below for responses to particular
> points...
>
I didn't grasp your precise problem but I've been having some similar
problems (without metaclasses however). Two tricks:
- Insert a dummy class into you heirarchy between object (or whatever
other superclass that doesn't have the properties yet and the class
where you install the properties. Then you might be able to use
this class to access the underliying class dict without being
intercepted by the properties. [I recall some problems with
properties vs. super, didn't follow them closely].
- If you use __slots__, they are also implemented as properties. So
if you define properties with the same name in the same class, you
lose all access to the raw slots. The solution is similar: define
__slots__ in a superclass, hide them with properties in a sublass.
See http://www.technion.ac.il/~cben/python/streams.py for an
example.
HTH,
Beni Cherniavsky <cben at tx.technion.ac.il>
More information about the Python-list
mailing list