[Tutor] Passing Functions or Code as Parameters

Kent Johnson kent37 at tds.net
Fri Oct 28 11:59:38 CEST 2005


Ed Singleton wrote:
> On 27/10/05, Kent Johnson <kent37 at tds.net> wrote:
> 
>>Ed Singleton wrote:
>>>Can I pass a block of code that will be executed within the function's
>>>scope?
>>
>>Yes, you can define a function within traverse() and pass that function to things_to_do(), but I don't know how that will help.
> 
> 
> I was thinking more of something I think I read in Ruby where you can
> pass a block of code to a function and it will perform the block of
> code at each iteration, and you can operate within the scope of the
> function.  (I think they were actually called blocks or something).

Ruby has code blocks which are passed to a function. They are widely used in Ruby. The closest equivalent in Python 2.4 is to pass a function to the other function. This has much the same capabilities as Ruby blocks but not as natural a syntax.

There is a proposal to add a similar capability to Python but it has not been finalized - see PEP 343:
http://www.python.org/peps/pep-0343.html

Kent
-- 
http://www.kentsjohnson.com



More information about the Tutor mailing list