Building a function call?

Peter Hansen peter at engcorp.com
Wed Jul 13 08:52:28 EDT 2005


Roland Heiber wrote:
> Not the best (not at all) but one way:
> 
> def dothat(x,y):
>   print "Called with:", x, y
> 
> c = (1,2)
> 
> locals().get("dothat")(*c)

As you say, not the best, but in fact not really advisable under any 
circumstances.  locals() returns "module level" stuff only when executed 
_at_ module level (i.e. not inside a function).  Otherwise it will 
return only the local variables of the frame its in.

Better is this, since it works for the OP's requirements in either 
situation.

   globals()['dothat'](*c)

-Peter



More information about the Python-list mailing list