On Mar 9, 4:26 pm, "Troels Thomsen" <nej tak ...> wrote: > Hello, > > I am writing a simple delayed-call mechanism , that is causing a bit of > headache. Works like this: > > myPrint(s) > print "..." + s > > myTimer.add(myPrint , "hello" , 15) > > This means that the myprint function is called in 15 seconds with the > parameter "hello". > The housekeeping of these timers is called by the main loop of the "os" > > This works well but i would like to be able to use it with any number of > parameters > > Functools is not a part of the 1.5.2+ python that I am running on (embedded > device), > so i tried to pass the parameters as a tuple like this > > myTimer.add(myAdder , (3,6) , 15) > > and the housekeeping function would then call the function like this > > def updateTimers() > for timerItm in timerTable: > ... > .... > .... > timerItm.func(*timerItm.parameters) > > Works well on python 2.5 but not on 1.5.2 (?) > > Current solution is to have default parameters None for the add function > > def add( func , timeout , param1 = None , param2 = None) > > And the update function then checks if parameters is specified > > def updateTimers() > for timerItm in timerTable: > ... > .... > .... > # ugly part : > if timerItm.param1 is not None and timerItm.param2 is not None: > timerItm.func(timerItm.param1, timerItm.param2) # two parameters > elif ...... > timerItm.func(timerItm.param1) # one parameter > else > timerItm.func() # no parameters > > This has the implication that I can not call a function with the parameter > None if I wanted to. > (not a huge problem) > > Right now it works quite well with up to two parameters, it covers 99% of > usage. If I need to call a function with more parameters, i can always write > a wrapper function for it. Wondering if anyone had some sugestions ? > > By the way, is it bad style to check for object identity instead of value > "None". > What aboutt integers ? if value is 0: .. > I guess it is implementation specific / could change in future versions ? > > Thx, > Troels
def g( arg1, arg2 ): print( arg1, arg2 ) return arg1 def h( arg1 ): print( arg1 ) return arg1 def caller( fun, *args, **kwargs ): return fun( *args, **kwargs ) print( caller( g, 'abc', 'def' ) ) print( caller( h, 'abc' ) ) ''' abc def abc abc abc ''' -- http://mail.python.org/mailman/listinfo/python-list