R
Robin Becker
Is there a way to override a data property in the instance? Do I need to create
another class with the property changed?
another class with the property changed?
Is there a way to override a data property in the instance? Do I need to
create another class with the property changed?
How do you need to "override" it? Care to create a toy example with aIs there a way to override a data property in the instance? Do I need to create
another class with the property changed?
a simple attribute is not a property in the sense Robin meant it,No, you can just do it on the fly. You can even create properties
(attributes) on the fly.
class Dummy:
property = True
d = Dummy()
d.property = False
d.new = True
Bruno said:Robin Becker a écrit :
Do you mean attributes or properties ?
Robin said:I mean property here.
My aim was to create an ObserverProperty class
that would allow adding and subtracting of set/get observers.
Robin Becker said: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.
......Could you elaborate ? Or at least give an exemple ?
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.
Robin said:Is there a way to override a data property in the instance? Do I need to create
another class with the property changed?
Kay said:It is possible to decorate a method in a way that it seems like
property() respects overridden methods. The decorator cares
polymorphism and accesses the right method.
def overridable(f):
def __wrap_func(self,*args,**kwd):
func = getattr(self.__class__,f.func_name)
if func.func_name == "__wrap_func":
return f(self,*args,**kwd)
else:
return func(self,*args,**kwd)
return __wrap_func
class A(object):
def __init__(self, x):
self._x = x
@overridable
def get_x(self):
return self._x
x = property(get_x)
class B(A):
def get_x(self):
return self._x**2
class C(B)ass
49
Robin said:## my silly example
class ObserverProperty(property):
def __init__(self,name,observers=None,validator=None):
self._name = name
self._observers = observers or []
self._validator = validator or (lambda x: x)
self._pName = '_' + name
property.__init__(self,
fset=lambda inst, value: self.__notify_fset(inst,value),
)
def __notify_fset(self,inst,value):
value = self._validator(value)
for obs in self._observers:
obs(inst,self._pName,value)
inst.__dict__[self._pName] = value
def add(self,obs):
self._observers.append(obs)
def obs0(inst,pName,value):
print 'obs0', inst, pName, value
def obs1(inst,pName,value):
print 'obs1', inst, pName, value
class A(object):
x = ObserverProperty('x')
a=A()
A.x.add(obs0)
a.x = 3
b = A()
b.x = 4
#I wish I could get b to use obs1 instead of obs0
#without doing the following
class B(A):
x = ObserverProperty('x',observers=[obs1])
b.__class__ = B
b.x = 7
Steven said:Robin Becker wrote:
........
Can you add the object to be observed as another parameter to the add
method?
py> class ObservableProperty(property):
... def __init__(self, *args, **kwargs): .......
py> A.x.add(b, obs2)
py> b.x = 7
obs2: 7
Probably "self._observers" should use some sort of weakref dict instead
of a regular dict, but hopefully the idea is clear.
STeVe
Robin said:I thought that methods were always overridable.
In this case the lookup on the
class changes the behaviour of the one and only property.
Kay said:Robin Becker wrote:
How can something be made overridable that is actually overridable? I
didn't know how to better express the broken polymorphism of Pythons
properties than by stating it as a pleonasm about the used get and set
methods. This way a property don't ever have to be redefined in
subclasses if get_x, set_x etc. are changed.
Kay
Sorry, but my news feed went belly-up for a few days and I had to go toI 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.
bruno modulix wrote:
.....
.....
in answer to Bengt & Bruno here is what I'm sort of playing with. Alex suggests
class change as an answer, but that looks really clunky to me. I'm not sure what
Alex means by
I see it's clear how to modify the behaviour of the descriptor instance, but is
he saying I need to mess with the descriptor magic methods so they know what
applies to each instance?
## my silly example
class ObserverProperty(property):
def __init__(self,name,observers=None,validator=None):
self._name = name
self._observers = observers or []
self._validator = validator or (lambda x: x)
self._pName = '_' + name
property.__init__(self,
fset=lambda inst, value: self.__notify_fset(inst,value),
)
obs = inst.__dict__.get('__override_'+obs.func_name, obs)def __notify_fset(self,inst,value):
value = self._validator(value)
for obs in self._observers:
obs(inst,self._pName,value)
inst.__dict__[self._pName] = value
def add(self,obs):
self._observers.append(obs)
def obs0(inst,pName,value):
print 'obs0', inst, pName, value
def obs1(inst,pName,value):
print 'obs1', inst, pName, value
class A(object):
x = ObserverProperty('x')
a.x = 3
b = A()
b.x = 4
I think your class assignment would eliminate all x-observing functions,#I wish I could get b to use obs1 instead of obs0
#without doing the following
b.x = 7
Robin Becker said:in answer to Bengt & Bruno here is what I'm sort of playing with. Alex
suggests class change as an answer, but that looks really clunky to me.
I'm not sure what
Alex means by
I see it's clear how to modify the behaviour of the descriptor instance,
but is he saying I need to mess with the descriptor magic methods so they
know what applies to each instance?
## my silly example
class ObserverProperty(property):
def __init__(self,name,observers=None,validator=None):
self._name = name
self._observers = observers or []
self._validator = validator or (lambda x: x)
self._pName = '_' + name
property.__init__(self,
fset=lambda inst, value: self.__notify_fset(inst,value),
)
def __notify_fset(self,inst,value):
value = self._validator(value)
for obs in self._observers:
obs(inst,self._pName,value)
inst.__dict__[self._pName] = value
def add(self,obs):
self._observers.append(obs)
def obs0(inst,pName,value):
print 'obs0', inst, pName, value
def obs1(inst,pName,value):
print 'obs1', inst, pName, value
class A(object):
x = ObserverProperty('x')
a=A()
A.x.add(obs0)
a.x = 3
b = A()
b.x = 4
#I wish I could get b to use obs1 instead of obs0
#without doing the following
class B(A):
x = ObserverProperty('x',observers=[obs1])
b.__class__ = B
b.x = 7
If (e.g.) __set__ needs to behave differently when applied to certain
instances rather than others, then it had better be "messed with"
(overridden) compared to property.__set__ since the latter has no such
proviso. Of course, your architecture as sketched below (taking
advantage of the fact that property.__set__ always calls a certain
callable, and you get to control that callable) is OK too.
Why not just fset=self.__notify_fset ? I fail to see the added value of
this lambda. Anyway...:
def __notify_fset(self,inst,value):
value = self._validator(value) ........
def add(self,obs):
self._observers.append(obs)
...this class only offers sets of observers *per-descriptor instance*,
not ones connected to a specific 'inst' being observed. My point is,
you could add the latter pretty easily. .......
.........
You can, if you have a way to call, say, b.x.add_per_inst(b, obs1).
Such as, adding within ObserverProperty:
self._observers_per_inst = {}
in the init, and changing the notification method to do:
def __notify_fset(self,inst,value):
value = self._validator(value)
observers = self._observers_per_inst.get(inst)
if not observers: observers = self._observers
for obs in observers:
obs(inst,self._pName,value)
inst.__dict__[self._pName] = value
and a new method add_per_inst:
def add_per_inst(self, inst, obs):
self._observers_per_inst.setdefault(inst,[]).append(obs)
Of course, you most likely want to use weak rather than normal
references here (probably to both instances and observers), but that's a
separate issue.
Robin said:well I guess that's the ambiguity of human language. Clearly when I
assign to a normal attribute I am changing its value; assigning to a
property or descriptor does something that is not so obvious. Changing
the behaviour of such an attribute could be done by inheritance as
suggested. The new class has overridden the property. When I want to do
that on an instance I have first to create a mutable version of the
descriptor where the mutability is on the instance not the class. I call
the action of changing the base descriptor behaviour 'overriding', but
perhaps that's not the correct word. What do you suggest?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.