How to create functors?

Leonhard Vogt leonhard.vogt at
Tue Aug 18 23:19:26 CEST 2009

> The example I gave earlier is a bit contrived, the real example
> fundamentally requires a lambda since I am actually passing in local
> variables into the functions the lambda is wrapping. Example:
> def MyFunction():
>   localVariable = 20
>   CreateTask( lambda: SomeOtherFunction( localVariable ) ) # CreateTask
> () executes the functor internally
> This is more or less like the real scenario I'm working with. There
> are other (more personal) reasons why I prefer to avoid 'def' in this
> case. I want to keep the functor as central to the code that needs it
> as possible to improve code readability.

what about

def MyFunction():
     localVariable = 20
     def wrapper():
         return SomeOtherFunction( localVariable )
     CreateTask( wrapper )

the wrapper is only visible inside MyFunction.


More information about the Python-list mailing list