override a property

Robin Becker robin at SPAMREMOVEjessikat.fsnet.co.uk
Sat Oct 22 04:44:28 EDT 2005


Alex Martelli wrote:
......
> 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.

..... I think I at last got this


......
> 
> Why not just fset=self.__notify_fset ?  I fail to see the added value of
> this lambda.  Anyway...:

duh just being Homerish

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

......
yes I begin at last to see the full complexity of this. There are really 
three possible attachments, the descriptor, instance class and the 
instance. Since the descriptor is attached to the class or a base class 
one could argue about whether observers should be inherited etc etc, but 
perhaps that's a step too far.

Thanks to Alex and Bengt and others for clarifying a bunch of issues.
-- 
Robin Becker



More information about the Python-list mailing list