Observer pattern thoughts
eppstein at ics.uci.edu
Mon Mar 10 21:50:30 CET 2003
george young <gry at ll.mit.edu> writes:
> > This scheme keeps the simplest interface, but allows one observer to
> > have as many different observables as wanted, with different update
> > functions. Or one update func with different attributes via a lamda curry:
> > ob1.register(lambda :ob.func(someval))
> > ob2.register(lambda :ob.func(otherval))
> > Am I missing something? Am I still suffering from C++ toxicity after
> > two years of pythonic bliss?
Bad idea to use lambdas as observer functions, at least like this. You
wouldn't be able to unobserve them, because the same lambda expression
will give an unequal value when you use it again.
You could work around this by storing the lambda in some persistent
state (global variable or object attribute) before registering it, but I
tend to think this pitfall is a problem with a function-based (as
opposed to method-based) observer API.
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
More information about the Python-list