Re: [Python-ideas] Lambda again: Anonymous function definition
On Mon, 31 Mar 2008 08:19:35, Eli Courtwright wrote:
On Mon, Mar 31, 2008 at 3:23 AM, Leszek Dubiel <leszek@dubiel.pl> wrote:
Lambda should have the same syntax as ordinary functions. The only difference should be: you don't have to put the name of the function.
def f (x, y): return x ** 2 + y ** 2
g = f
h = def (x, y): return x ** 2 + y ** 2
Functions f, g and h are doing the same.
Javascript handles anonymous functions this way as well:
function f(x, y) { return x*x + y*y; }
g = f;
h = function(x, y) { return x*x + y*y; }
With that being said, it makes sense for the return statement to be omitted in lambdas (or anonymous defs, as I hope they will eventually be called), since those functions are limited to one statement.
- Eli
+1 to handling anonymous functions/lambdas the way JavaScript does. It's the only thing I like better about JavaScript than Python. I don't know if I agree about leaving off the return statement, though. -matt
Matt Chisholm wrote:
+1 to handling anonymous functions/lambdas the way JavaScript does. It's the only thing I like better about JavaScript than Python.
Unfortunately, there's no way in Python to have a statement inside of an expression (because statements are delimited by line ends and indentation). Many people have attempted this, none have succeeded. Don't go there, you'll just be opening up old wounds... -- Talin
Talin wrote:
Matt Chisholm wrote:
+1 to handling anonymous functions/lambdas the way JavaScript does. It's the only thing I like better about JavaScript than Python.
Unfortunately, there's no way in Python to have a statement inside of an expression (because statements are delimited by line ends and indentation). Many people have attempted this, none have succeeded. Don't go there, you'll just be opening up old wounds...
From my experience anonymous functions are very useful when writing event-based code, such as client-side event handlers for web browsers with JavaScript, Expect event handlers in Tcl/Perl, and GUI event handlers in any language. I think having a simple, readable way to define anonymous functions in Python would allow writing more concise and readable Python code for such applications, thus making Python an even better Jack-of-all-trades.
However, even if it were easy to implement with clear, simple syntax, I'm not sure this would be a good idea; I'd leave this to more experienced language developers. - Tal
On Wed, Apr 2, 2008 at 9:49 PM, Talin <talin@acm.org> wrote:
Matt Chisholm wrote:
+1 to handling anonymous functions/lambdas the way JavaScript does. It's the only thing I like better about JavaScript than Python.
Unfortunately, there's no way in Python to have a statement inside of an expression (because statements are delimited by line ends and indentation). Many people have attempted this, none have succeeded. Don't go there, you'll just be opening up old wounds...
Well what's being proposed is for def f(x, y): return x*x + y*y to be a statement, but for def(x,y): x*x + y*y to be an expression. In other words, anonymous def will behave exactly like lambda does now. This seems doable, though I don't know Python's parsing code well enough to understand how difficult it would be to implement. Of course, we all understand that lambda won't change anytime soon; this is really more of a hypothetical "what if we did this in 10 years if there's ever a Python 4000" discussion. - Eli
participants (4)
-
Eli Courtwright
-
Matt Chisholm
-
Tal Einat
-
Talin