How to create functors?

Diez B. Roggisch deets at
Tue Aug 18 23:24:38 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:

funcs = []

for i in xrange(10):

    def f(i=i):
        print i


for f in funcs:

> 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.

Didn't you say the other day you came from C++? Given the galactic 
amount of hoops to jump through that language has to offer, typing

   def f

instead of the even longe


strikes me as rather peculiar.


More information about the Python-list mailing list