question about what lamda does

Nick Vatamaniuc vatamane at gmail.com
Tue Jul 18 07:47:07 EDT 2006


Use it anywhere a quick definition of a function is needed that can be
written as an expression. For example when a callback function is
needed you could say:
def callback(x,y):
  return x*y
some_function(when_done_call_this=callback)
But with lambda you could just write
some_function(when_done_call_this=lambda x,y:x*y)
Note: because it is an _expression_ you cannot do stuff like 'if..else'
inside of lambda.

-Nick V.

nephish at xit.net wrote:
> Hey there,
> i have been learning python for the past few months, but i can seem to
> get what exactly a lamda is for. What would i use a lamda for that i
> could not or would not use a def for ? Is there a notable difference ?
> I only ask because i see it in code samples on the internet and in
> books.
> 
> thanks for any clarity
> 
> sk




More information about the Python-list mailing list