How to create functors?

J. Cliff Dyer jcd at sdf.lonestar.org
Wed Aug 19 19:01:16 EDT 2009


On Wed, 2009-08-19 at 15:56 +0200, Bruno Desthuilliers wrote:
> Terry Reedy a écrit :
> > Robert Dailey wrote:
> > 
> >> I'm using Python 2.6. And using the legacy syntax in the lambda does
> >> not work either. I want to avoid using a def if possible. Thanks.
> > 
> > In Python, writing
> > 
> > name = lambda arg: expr
> > 
> > instead of
> > 
> > def name(arg): return expr
> > 
> > is all negative and no positive and should be avoided.
> 
> Except that def is a statement, and as such can't be used as a named 
> params when calling a function expecting a callback, ie:
> 
> vroom = some_dead('parrot', name=lambda arg: exp)
> 
> (notice the 'name = lambda arg: exp' ?-)
> 

Which offers no added functionality over:

  def name_func(arg):
      return exp

  vroom = some_dead('parrot', name=name_func)

except for confusion in debugging. :)  (See other excessively long
threads on lambda for further discussion of the debugging headaches
caused by lambdas.


> Ok, nitpicking. Me --->[]
> 
> <g>




More information about the Python-list mailing list