In article <4bf5e19e$0$27861$c3e8...@news.astraweb.com>, Steven D'Aprano <st...@remove-this-cybersource.com.au> wrote: >Sorry for breaking threading, but Stef's original post has not come >through to me. > >> On Thu, May 20, 2010 at 8:13 PM, Stef Mientki <stef.mien...@gmail.com> >> wrote: > >>> So I want to change the behavior of the class dynamically. I've done it >>> by adding a global variable (Base_Grid_Double_Click) in the module, >>> initial set to None, >>> but can be changed by the main program to some callback function. (see >>> the code below) > >How is this supposed to work? If you have *one* global, then *every* >instance will see the same setting. To change it dynamically, you enter a >nightmare world of having to save the global, modify it, then restore it, >every single time. Trust me, I've been there, this is the *worst* way of >programming. This is why object oriented inheritance was invented, to >escape this nonsense! > >The first thing is to make the callback specific to the class, not >global. Why does your printing code need access to the callback that >handles double-clicking on a grid? It doesn't! So don't give it that >access (at least, not easy access). Put the callback in the class. > >class MyClass: > callback = None > def method(self, *args): > if self.callback is None: > behaviour_with_no_callback() > else: > behaviour_with_callback() > > >Now if you want to apply a callback to some instances, and not others, it >is totally simple: > > >red = MyClass() >blue = MyClass() >red.callback = my_callback_function > >and you're done.
Don't go overboard in complication. Python can handle variable functions just fine, if that is the proper solution to your problem. (In c they are called function pointers and they are unruly.) def a(b,c): return b+c def p(q,r): return q*r x=a x(3,5) 8 x=p x(3,5) 15 <SNIP> >-- >Steven Sorry, but couldn't respond to the original message. My newsserver doesn't have it anymore. Groetjes Albert -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. alb...@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst -- http://mail.python.org/mailman/listinfo/python-list