[Tutor] "Dispatching" functions with args

jfouhy at paradise.net.nz jfouhy at paradise.net.nz
Thu Apr 14 02:14:07 CEST 2005


Quoting Luke Jordan <luke.jordan at gmail.com>:

> But this does not:
> 
>  def aFunc(configArg):
>  print "aFunc with argument"
>  print configArg
> 
>  def anotherFunc(configArg):
>  print "anotherFunc with argument"
>  print configArg 
>  return aFunc,1
   ^^^^^^^^^^^^^^

>  def dispatch(func,configArg):
>  while func is not None and configArg is not None:
>  func = func(configArg)
> 
>  dispatch(anotherFunc,1)
> 

The line I have underlined is returning a tuple.  So, when you do 

func = func(configArg)

It has the effect of setting 

func = (aFunc, 1)

which you then try to call.

Instead, you could try:

func, configArg = func(configArg)

Of course, you would then need to ensure that any function you could call (such
as aFunc) also returns an appropriate tuple, otherwise the unpacking will fail.
(or wrap it in an if statement or a try:except block or something)

-- 
John.


More information about the Tutor mailing list