Live data from Hacker News

Stop writing lambda expressions in Python

treyhunner.com

91–100 of 202 posts

Re: Stop writing lambda expressions in Python

#91
post #72

Personally, I use them often and disagree with a lot of this sentiment. I also disagree with a significant portion of PEP8, however. Anonymous functions are vital to any modern high level language. I think most people get hung up on the word. If the keyword was changed from “lambda” to “fun” for example I think it wouldn’t be as obtuse to more intermediate developers. To be honest I think the lambda does Python a ser…

> Anonymous functions are vital to any modern high level language. I don’t think you’re wrong, but I am also not experienced enough to know why this would be the case. What is the virtue of having an anonymous function? I get that it makes code somewhat easier to read and write if you’re only using the function once and it’s short, but that seems like an edge case. What am I missing?

I don't know an easy answer to this, but I recommend reading SICP: https://mitpress.mit.edu/sites/default/files/sicp/index.html

Re: Stop writing lambda expressions in Python

#92
post #65

Earlier quoted context omitted.

> 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 unders…

okay, f# has curly braces. but not in the sense of other C-like languages like c++, java, and c#. they are primarily used for sequence expressions and computation expressions like async. > 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 buil…

>>> test(2) is None

True

Re: Stop writing lambda expressions in Python

#93

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.

Which languages?

Re: Stop writing lambda expressions in Python

#94
post #68

Earlier quoted context omitted.

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.

> 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.

as explained in the interview, the course changed to python because the course itself changed. they basically killed off the old course and created new courses built around their new degree program structure. the new course hits a lot of different aspects of electrical, computer, and software engineering, many of which heavily use existing libraries, so they seemed to pick python for this.

in my opinion, they could have easily written libraries in another language, so i suspect there were some politics at MIT that lead to the decision to use python.

Re: Stop writing lambda expressions in Python

#95
post #63

Earlier quoted context omitted.

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…

thank you for clarifying exactly what's going on. other than the __qualname__, in what other ways do the two different bindings differ from each other? however, this still goes in the bucket of why i personally dislike python. this just seems sloppy and overly complicated. for example, i tested this out myself. > test = lambda x : x*x > test => at 0x7ff6221d76e0> > test.__qualname__ Traceback (most recent call last):…

FYI, repl.it has Python3, you just need to select Python3 and not Python.

As far as I know, the __qualname__ property was introduced in Python3.3, so obviously you wouldn't see it in any Python 2 version.

Re: Stop writing lambda expressions in Python

#96
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…

Just as in f#, Python's lambdas can be bound to a name. There's no real difference between a def and a lambda. You're complaining about some distinction that doesn't exist. You didn't even try to discover whether it does before complaining about it.

Re: Stop writing lambda expressions in Python

#97
post #63

Earlier quoted context omitted.

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…

thank you for clarifying exactly what's going on. other than the __qualname__, in what other ways do the two different bindings differ from each other? however, this still goes in the bucket of why i personally dislike python. this just seems sloppy and overly complicated. for example, i tested this out myself. > test = lambda x : x*x > test => at 0x7ff6221d76e0> > test.__qualname__ Traceback (most recent call last):…

Van Rossum in https://python-history.blogspot.com/2009/04/origins-of-pytho... comments that "lambda" was likely the wrong choice for a name, saying "the choice of terminology had many unintended consequences. For instance, users familiar with functional languages expected the semantics of lambda to match that of other languages. As a result, they found Python’s implementation to be sorely lacking in advanced features."

Perhaps it would help if you didn't think of "lambda" as defining a lambda function, but something more prosaic like "defexpr", and that Python doesn't have lambda functions at all?

As to your experiments, you have found one of the many differences between Python 2 and Python 3.

After researching F# for about 10 minutes, perhaps you might think of Python 2 as being like OCaml and Python 3 being like F# - they are two instances of the same language family, with a mutually compatible language subset, but many incompatibilities.

Re: Stop writing lambda expressions in Python

#98
post #92

Earlier quoted context omitted.

okay, f# has curly braces. but not in the sense of other C-like languages like c++, java, and c#. they are primarily used for sequence expressions and computation expressions like async. > 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 buil…

>>> test(2) is None True

i guess that is true in python3 but it is not in python 2.7.

Re: Stop writing lambda expressions in Python

#99
post #63

Earlier quoted context omitted.

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…

thank you for clarifying exactly what's going on. other than the __qualname__, in what other ways do the two different bindings differ from each other? however, this still goes in the bucket of why i personally dislike python. this just seems sloppy and overly complicated. for example, i tested this out myself. > test = lambda x : x*x > test => at 0x7ff6221d76e0> > test.__qualname__ Traceback (most recent call last):…

Don't complain about design warts if you're using an old version. Python 2 is just a couple years from end-of-life, and that's after the support guarantee was extended.

Re: Stop writing lambda expressions in Python

#100
post #4

all i could think of is "stop writing python". it seems to me that a lot of these problems are fundamental problems with python. i continue to not understand why anybody likes python the language. the fact that creating a function with lambda versus the normal way is different is bonkers. for example, in f# (and other sane languages), the following are identical: let test1 = fun x -> x * x let test2 x = x * x both re…

I agree with you, I felt the same when reading the article. It feels that Python's design is deeply at fault here, it is actively harming people's perception of anonymous functions, which is the opposite of what a supposedly educational multi-paradigm-capable language should be doing.
Post reply on HN