Live data from Hacker News

Stop writing lambda expressions in Python

treyhunner.com

61–70 of 202 posts

Re: Stop writing lambda expressions in Python

#61

Earlier quoted context omitted.

it's because i am not that deeply familiar with python and the article is not clear itself on this. what is he complaining about then if they are identical in what's returned? is it simply because the REPL returns instead of the function name? why does the REPL do this? are they different in some subtle way? the point was in something like f#, lambda creates a function which can then be bound to a name if you want. i…

> is it simply because the REPL returns instead of the function name? why does the REPL do this? Because lambdas don't have function names.

i know that. in the example, he assigned the lambda to normalize_case.

Re: Stop writing lambda expressions in Python

#62
Python really needs much better anonymous functions - Lambda expressions just don't cut it.

To be clear, what Python does need is multiline, inline anonymous functions. In JavaScript/ES2015, the fat arrow function syntax is just remarkably powerful in its ability to simplify code. I would go so far as to say that the ES2015 fat arrow syntax completely changed the way my programs are written, increasing power and reducing complexity. ES2015 fat arrows I think are possible the best programming language feature I know.

async programming in JavaScript benefits massively from the beautifully terse and powerful anonymous functions in ES2015. Python now has powerful async programming, but no equivalent to the ES2015 terse inline function anonymous function syntax.

I raised this issue elsewhere once and someone said "there will never be multiline, inline anonymous functions in Python because it would require brackets". If so then it is a great pity that Python is simply not capable of including a language feature that is IMO absolutely critical.

Also, as mentioned elsewhere in this thread, using the word "lambda" to describe anonymous functions is a really really bad decision. Lambda sounds very deep, very computer sciencey and very complex. Probably they should just be called anonymous functions in keeping with the rest of the industry, which also is not a great name but is much better than "Lambda" functions.

Beginners might think along these lines: "Lambda .... lambda calculus? I don't know calculus. I'm outta here."

Names matter.

Re: Stop writing lambda expressions in Python

#63

Earlier quoted context omitted.

> is it simply because the REPL returns instead of the function name? why does the REPL do this? Because lambdas don't have function names.

i know that. in the example, he assigned the lambda to normalize_case.

Sure, but an object doesn't know what variable it is assigned to (and which variable name should it pick if there's multiple).

the def syntax merely sets the name property on the function object too, since it has a name to set available. As said in other comments, nice for interactive use, irrelevant otherwise.

    >>> normalize_case = lambda s: s.casefold()
    >>> normalize_case
     at 0x034B04B0>
    >>> normalize_case.__qualname__ = "normalize_case"
    >>> normalize_case
    
vs

    >>> def normalize_case(s):
	return s.casefold()

    >>> normalize_case
    
    >>> normalize_case.__qualname__
    'normalize_case'

Re: Stop writing lambda expressions in Python

#64

I don't write Python. But I do program in a few different languages (Ruby, Elixir, ES6, CoffeeScript, Rust...) Is this a thing? Are Lambdas "Unpythonic?" or is the writer of the article totally off base?

The author is entitled to his perspective, but I would equate this article to JS articles titled "A framework author's advice to avoid frameworks". Its a trendy article because it takes something that a lot of people use and describes its weaknesses. Fun to read and argue with, but not a mainstream idea.

Re: Stop writing lambda expressions in Python

#65
post #25

Earlier quoted context omitted.

> i continue to not understand why anybody likes python the language Here are some reasons why I like it: It's cleaner to read because there aren't curly braces and semicolons cluttering up the code. It's cleaner to write because the expressions are simpler and look like something readable instead of line noise. It has a REPL, so it's easy to experiment. It doesn't force me to write boilerplate code, such as types fo…

f# (and also ocaml and sml) have all those and much more. they are much more consistent and principled in their design, which makes things simpler to understand. for example, in all three languages above, expressions are much easier to understand because they always return a value. this makes code easier to reason about. this is not true in python. the languages i mentioned do have static typing but they have type in…

> f# (and also ocaml and sml) have all those and much more

F# has curly braces. Also, as you say, it requires some type annotations.

I see that F# has a REPL, but it seems bolted on as an afterthought instead of being an integral part of the language. (Also, it's not clear whether the REPL is available in Mono on Linux. I am allergic to Windows and anything built by Microsoft.)

> expressions are much easier to understand because they always return a value. this makes code easier to reason about. this is not true in python

Huh? Python expressions always return a value.

Perhaps what you mean is that Python also has statements, which do not return a value, whereas these languages do not--everything is an expression, as in Lisp?

> you owe it to yourself to learn an ml

I don't owe it to myself to learn anything unless I decide to. I wasn't trying to convince anyone else to use Python; I was just giving reasons why I like Python. Please extend me the same courtesy when you talk about things you like about your favorite languages.

Re: Stop writing lambda expressions in Python

#66
post #32

Earlier quoted context omitted.

I'm not sure what you mean by the first half of your comment. Both def and lambda create functions, and quoting from the article "Lambda expressions are just a special syntax for making functions. They can only have one statement in them and they return the result of that statement automatically.". def binds it to a name at the same time, which is remembered in the object (which AFAIK isn't really used for anything e…

it's because i am not that deeply familiar with python and the article is not clear itself on this. what is he complaining about then if they are identical in what's returned? is it simply because the REPL returns instead of the function name? why does the REPL do this? are they different in some subtle way? the point was in something like f#, lambda creates a function which can then be bound to a name if you want. i…

Modules and functions in Python have a __name__ attribute. If you've used Python for scripts, you've probably written `if __name__ == "__main__":` at some point, which is the module name for executed scripts, whereas imported modules get their proper name.

Similarly, Python basically keeps track of "proper" names for proper functions, but can't/won't do so for lambdas.

Re: Stop writing lambda expressions in Python

#67

I don't write Python. But I do program in a few different languages (Ruby, Elixir, ES6, CoffeeScript, Rust...) Is this a thing? Are Lambdas "Unpythonic?" or is the writer of the article totally off base?

Guido van Rossum actually considered removing lambdas in Python 3. See https://www.artima.com/weblogs/viewpost.jsp?thread=98196

Though it didn't happen in the end: https://softwareengineering.stackexchange.com/a/252162/29892...

Re: Stop writing lambda expressions in Python

#68
post #26

Earlier quoted context omitted.

> i read an interview with hal abelson where he called python's lambda broken Can you give a link? I'd be interested to read it.

sure. here you go. http://www.gigamonkeys.com/code-quarterly/2011/hal-abelson/ the paragraph he says it in is: > It’s just a different course. We could have done it in Scheme and I sort of wish we had. And for random reasons we didn’t. But the thing that ties it together—if you had fifteen seconds to describe the whole course—it’s still about abstraction and modularity. The beginning of that course is not very differ…

Thanks for the link. The paragraph you quote makes me think he is comparing Python's lambda to Scheme's equivalent, which of course will make Python's lambda seem broken. IIRC the course was taught in Scheme at one time, but it switched to Python because it seemed easier for students to grasp the basics that way.

Re: Stop writing lambda expressions in Python

#69

Python really needs much better anonymous functions - Lambda expressions just don't cut it. To be clear, what Python does need is multiline, inline anonymous functions. In JavaScript/ES2015, the fat arrow function syntax is just remarkably powerful in its ability to simplify code. I would go so far as to say that the ES2015 fat arrow syntax completely changed the way my programs are written, increasing power and redu…

FWIW, in many languages, "lambda expressions" and "anonymous functions" are two similar but different things. Specifically, a lambda expression is a shorthand syntax for writing anonymous functions whose body consists of a single expression.

Which is pretty much what you get in Python.

Re: Stop writing lambda expressions in Python

#70

Python really needs much better anonymous functions - Lambda expressions just don't cut it. To be clear, what Python does need is multiline, inline anonymous functions. In JavaScript/ES2015, the fat arrow function syntax is just remarkably powerful in its ability to simplify code. I would go so far as to say that the ES2015 fat arrow syntax completely changed the way my programs are written, increasing power and redu…

FWIW, in many languages, "lambda expressions" and "anonymous functions" are two similar but different things. Specifically, a lambda expression is a shorthand syntax for writing anonymous functions whose body consists of a single expression. Which is pretty much what you get in Python.

Lambdas aren't single expression functions in Lisp.
Post reply on HN