override a property
Alex Martelli
aleaxit at yahoo.com
Tue Oct 18 04:50:08 EDT 2005
Robin Becker <robin at SPAMREMOVEjessikat.fsnet.co.uk> wrote:
> Bruno Desthuilliers wrote:
> > Robin Becker a écrit :
> >
> >> Is there a way to override a data property in the instance? Do I need
> >> to create another class with the property changed?
> >
> > Do you mean attributes or properties ?
>
> I mean property here. My aim was to create an ObserverProperty class
> that would allow adding and subtracting of set/get observers. My current
> implementation works fine for properties on the class, but when I need
> to specialize an instance I find it's quite hard.
A property is an 'overriding descriptor', AKA 'data descriptor', meaning
it "captures" assignments ('setattr' kinds of operations), as well as
accesses ('getattr' kinds), when used in a newstyle class. If for some
reason you need an _instance_ to bypass the override, you'll need to set
that instance's class to one which has no overriding descriptor for that
attribute name. A better design might be to use, instead of the builtin
type 'property', a different custom descriptor type that is specifically
designed for your purpose -- e.g., one with a method that instances can
call to add or remove themselves from the set of "instances overriding
this ``property''" and a weak-key dictionary (from the weakref module)
mapping such instances to get/set (or get/set/del, if you need to
specialize "attribute deletion" too) tuples of callables.
Alex
More information about the Python-list
mailing list