Re: Observer-Pattern by (simple) decorator

2007-06-02 Thread Wildemar Wildenburger
Steven Bethard wrote: > Wildemar Wildenburger wrote: > > class Observable(object): > >> ... def __init__(self, func, instance=None, observers=None): >> ... self.func = func >> ... self.instance = instance >> ... self.observers = observers or [] >>

Re: Observer-Pattern by (simple) decorator

2007-06-02 Thread Steven Bethard
Wildemar Wildenburger wrote: class Observable(object): > ... def __init__(self, func, instance=None, observers=None): > ... self.func = func > ... self.instance = instance > ... self.observers = observers or [] Unless you also changed code in __get__, this means yo

Re: Observer-Pattern by (simple) decorator

2007-06-02 Thread Wildemar Wildenburger
Peter Otten wrote: class Descriptor(object): > ... def __get__(*args): return object() > ... > class SomeActor(object): > ... meth = Descriptor() > ... > SomeActor.meth is SomeActor.meth > False > Of course ... Man, this lang

Re: Observer-Pattern by (simple) decorator

2007-06-02 Thread Steven Bethard
David Wahler wrote: > On Jun 2, 12:27 am, Steven Bethard <[EMAIL PROTECTED]> wrote: >> I think you want to define __get__ on your Observable class so that it >> can do the right thing when the method is bound to the instance: [snip] > Is this desired behavior? > a = SomeActor() b = SomeA

Re: Observer-Pattern by (simple) decorator

2007-06-02 Thread Peter Otten
Wildemar Wildenburger wrote: > Thanks for the thorough explanation. But you did mock me with that > "SomeActor.meth is SomeActor.meth"-line, right? ;) Acually, no: >>> class Descriptor(object): ... def __get__(*args): return object() ... >>> class SomeActor(object): ... meth = Descriptor

Re: Observer-Pattern by (simple) decorator

2007-06-02 Thread Wildemar Wildenburger
David Wahler wrote: > > Here's my attempt at an implementation that works as a decorator. It > stores the observers in a dictionary attribute of the instance, using > the method name as a key. For example: > > > [snip: another nice aproach] > Anyway, these are just my initial thoughts -- I don't

Re: Observer-Pattern by (simple) decorator

2007-06-02 Thread Wildemar Wildenburger
Peter Otten wrote: > Then you have modified the code posted by Steven Bethard. > > Oops. Indeed I did. I changed this >>> class Observable(object): ... def __init__(self, func, instance=None, observers=None): ... if observers is None: ... observers = [] ... self.

Re: Observer-Pattern by (simple) decorator

2007-06-02 Thread David Wahler
On Jun 2, 3:00 pm, Peter Otten <[EMAIL PROTECTED]> wrote: > Then you have modified the code posted by Steven Bethard. > > > I don't see how your behaviour should come about ... a new observer-list > > is created for every decorated method, so there is no problem. > > Yes, but that list is shared a

Re: Observer-Pattern by (simple) decorator

2007-06-02 Thread Peter Otten
Wildemar Wildenburger wrote: > David Wahler wrote: >> Is this desired behavior? >> >> > a = SomeActor() > b = SomeActor() > a.meth.observers is b.meth.observers > >> True >> > No it's not, but I get > a = SomeActor() b = SomeActor() a.meth.observers i

Re: Observer-Pattern by (simple) decorator

2007-06-02 Thread Wildemar Wildenburger
David Wahler wrote: > Is this desired behavior? > > a = SomeActor() b = SomeActor() a.meth.observers is b.meth.observers > True > No it's not, but I get >>> a = SomeActor() >>> b = SomeActor() >>> a.meth.observers is b.meth.observers False I don't see how your

Re: Observer-Pattern by (simple) decorator

2007-06-02 Thread David Wahler
On Jun 2, 12:27 am, Steven Bethard <[EMAIL PROTECTED]> wrote: > I think you want to define __get__ on your Observable class so that it > can do the right thing when the method is bound to the instance: > > >>> class Observable(object): > ... def __init__(self, func, instance=None, observers=No

Re: Observer-Pattern by (simple) decorator

2007-06-01 Thread Gabriel Genellina
En Fri, 01 Jun 2007 21:25:50 -0300, Wildemar Wildenburger <[EMAIL PROTECTED]> escribió: > Steven Bethard wrote: >> I think you want to define __get__ on your Observable class so that it >> can do the right thing when the method is bound to the instance: > > It took me quite some time understand

Re: Observer-Pattern by (simple) decorator

2007-06-01 Thread Wildemar Wildenburger
Steven Bethard wrote: > I think you want to define __get__ on your Observable class so that it > can do the right thing when the method is bound to the instance: > > >>> class Observable(object): > [snip] > ... def __get__(self, obj, cls=None): > ... if obj is None: > ... re

Re: Observer-Pattern by (simple) decorator

2007-06-01 Thread Steven Bethard
Wildemar Wildenburger wrote: > I thought: I'll just write a decorator that lets me react to method > calls easily (the ever so popular observer-pattern). I've looked at some > recepies, but I just don't get them (I'm feeling kinda dumb today, sorry). [snip] > This is more complicated than expecte

Observer-Pattern by (simple) decorator

2007-06-01 Thread Wildemar Wildenburger
Hello folks :) This has got to be an FAQ, but somehow I can't seem to find an answer that I understand ... I thought: I'll just write a decorator that lets me react to method calls easily (the ever so popular observer-pattern). I've looked at some recepies, but I just don't get them (I'm feelin