How to create functors?

Diez B. Roggisch deets at nospam.web.de
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

    funcs.append(f)

for f in funcs:
    f()


> 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

   lambda

strikes me as rather peculiar.

Diez



More information about the Python-list mailing list