Re: question about what lamda does



nephish@xxxxxxx 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.

It defines a function.

f = lambda x, y: expression

is equivalent to

def f(x, y):
return expression

Note that lambda is an expression while def is a statement.

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.

Lambdas are typically used as parameters to functions that take
functions as arguments, like property() and reduce(). You never *need*
to use one, but sometimes it's convenient.

.



Relevant Pages

  • Re: Extending Python Syntax with @
    ... as I do now and as people learning Python will know when ... theory behind the lambda calculus. ... squeeze functions into a tight space: ... def f2: return 2**x ...
    (comp.lang.python)
  • Re: Default parameters
    ... The default parameter value bindings are made a def time. ... The comparable def code to your lambda would be ... >One might argue that this could could benefit from resolving ...
    (comp.lang.python)
  • Re: whats wrong with "lambda x : print x/60,x%60"
    ... >> One perhaps needs to be a little more careful with instance variables, ... > def callback(): ... But this seems to defeat the main argument for removing lambda ...
    (comp.lang.python)
  • Re: Anonymous methods, blocks etc. (Cont. default block params)
    ... > room was that either using defor lambda() was generally felt to be ... Using def(), however, means that "def" is ... non-closure is at all more efficient that's agood thing. ... > much magic punctuation in Ruby as it stands now. ...
    (comp.lang.ruby)
  • Re: How to create functors?
    ... variables into the functions the lambda is wrapping. ... def MyFunction: ... CreateTask(lambda: SomeOtherFunction(localVariable)) # CreateTask ... >> It looks like in your version of Python "print" isn't a function. ...
    (comp.lang.python)