Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Lambda Functions in Python
06-22-2010, 11:28 PM (This post was last modified: 06-22-2010 11:36 PM by Alphablend.)
Post: #1
Thumbs Up Lambda Functions in Python
These lambda functions can be used when you have to write a function, without defining it somewhere in you code. Like in interface building when assigning a function to a button or something. They are some kind of one-line funtions.

Here is an example of normal function f() and after it an example of Lambda function g(). The result is the same.

Code:
>>> def f(x): return x*x-1
...
>>> f(6)
35
>>> g=lambda x:x*x-1
>>> g(6)
35

[Image: 45669_pythonlogo.png][Image: 45668_javalogo.png]
Find all posts by this user
Quote this message in a reply
06-22-2010, 11:34 PM
Post: #2
RE: Lambda Functions
Very good, THX for sharing this piece of code. That is easy to understand though. But with lambda you can only write small functions.
The only thing i don't know about this is weather lambda in Python is a preprocessor directive ???

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
01-06-2011, 04:29 PM
Post: #3
RE: Lambda Functions in Python
Is there even a Python preprocessor? Wink

http://stackoverflow.com/questions/89012...lambda-why
http://docs.python.org/tutorial/controlf...mbda-forms
http://www.secnetix.de/olli/Python/lambd...tions.hawk

What I'd want to see is JavaScript style lambda - full fledged anonymous function whenever I want it, not just a single expression. Ah well, that would mess around with indentation a bit...
Find all posts by this user
Quote this message in a reply
01-06-2011, 07:51 PM
Post: #4
RE: Lambda Functions in Python
(01-06-2011 04:29 PM)Xupicor Wrote:  Is there even a Python preprocessor? Wink
No, there is not.

(01-06-2011 04:29 PM)Xupicor Wrote:  What I'd want to see is JavaScript style lambda - full fledged anonymous function whenever I want it, not just a single expression. Ah well, that would mess around with indentation a bit...

You can define a function anywhere in your code, even inside another function. You just need to give it a name, but it will create a new function whenever the code is run, just as in JS with similar scope rules.

Please note that you the lambda cannot hold a singe statement, it can only hold an expression. It is not possible to assign anything inside a lambda, nor is it possible to use the semicolon. What you can do however, is use lots of nasty tricks or boolean-based short-circuiting to implement if/else structures and the likes. To do assignments, you need to work with lists or dictionaries and use the methods to set the elements to a certain value.
Find all posts by this user
Quote this message in a reply
01-07-2011, 02:53 AM
Post: #5
RE: Lambda Functions in Python
About the preprocessor, yes, I know. Actually I think there were some projects floating around the net that implemented Python preprocessor to some degree, but I fail to see the point in that, since the language is so flexible...
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: