Question on passing function as argument

Greg Ewing (using news.cis.dfn.de) ckea25d02 at sneakemail.com
Mon Mar 31 21:01:34 EST 2003


matthew wrote:
> but if I do:-
> 
> event.add_listener('linear', [calc_position(params)])
> 
> the function seems to execute 'in-place' and the event_list[event] shows 
> None.

If your function didn't require the params argument, you
would be able to pass it in *without* calling it by
doing

    event.add_listener('linear', [calc_position])

But because you want to supply an argument to the
function before passing it in, you need what is
known in the jargon as a "curried function". You
can do this in Python as follows:

   def calc_position_listener(params):
     def listener():
       calc_position(params)
     return listener

and then

   event.add_listener('linear', [calc_position_listener(params)])

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg





More information about the Python-list mailing list