Live data from Hacker News

Stop writing lambda expressions in Python

treyhunner.com

121–130 of 202 posts

Re: Stop writing lambda expressions in Python

#121
post #36
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…

These are good reasons for why people might prefer python over other imperative languages, though basically all these points also apply to Haskell/OCaml/F#/etc., though some people would still say that a lot of things there look like line noise. I think the GP's point was that functional languages give you similar benefits, but with more uniform and disciplined behavior of many language features. Why FP is not more w…

I can only speak for myself and I'm more interested in getting a job done. If that means my UI component uses class syntax, and my state and communications channels are functional reducers, so be it.

I love JS because it's flexible and doesn't lock me into a paradigm. I can be as strict as I want to be. In the end, I think it depends on how someone thinks about problems. I tend to bend my thinking depending on the type of problem and how I might optimally solve said problem. When I need something else (often for performance), I'll circle around at that point for that part.

Re: Stop writing lambda expressions in Python

#122
post #102

Earlier quoted context omitted.

False. You're just not familiar with how the REPL works. >>> None >>> print(None) None In the future, you might want to refrain from strong criticism of a thing you're not familiar with. That habit leads to all sorts of -isms.

i do know how the repl works. i explicitly tried it out. i defined the function just as i said and ran test(2) is None and got false. i just now went to try it out again at home and got true. i don't know what to tell you other than something must have got shadowed somewhere.

Here's what I get from the Python 3 REPL:

    peter@localhost:~$ python3
    Python 3.5.2 (default, Nov 23 2017, 16:37:01)
    [GCC 5.4.0 20160609] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> def test(x):
    ...     "something"
    ...
    >>> test(2) is None
    True
And Python 2:

    peter@localhost:~$ python
    Python 2.7.12 (default, Dec  4 2017, 14:50:18)
    [GCC 5.4.0 20160609] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> def test(x):
    ...     "something"
    ...
    >>> test(2) is None
    True
If you post such a transcript from your REPL session where you get False, it should be easier to tell what's different between your setup and mine (which is a stock Python install on Ubuntu 16.04).

However, there is another even more direct experiment you could have run at the Python REPL to see what Python expressions return: just type the expression directly at the REPL and see what it prints back at you. In Python 3:

    peter@ToshibaSatellite:~$ python3
    Python 3.5.2 (default, Nov 23 2017, 16:37:01)
    [GCC 5.4.0 20160609] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> "something"
    'something'
And in Python 2:

    peter@ToshibaSatellite:~$ python
    Python 2.7.12 (default, Dec  4 2017, 14:50:18)
    [GCC 5.4.0 20160609] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> "something"
    'something'
So, in your test function above, the expression "something" does return itself; it just does it inside the function, and that return value then gets thrown away when the function exits, because you didn't return it from the function itself.

Re: Stop writing lambda expressions in Python

#123

Earlier quoted context omitted.

Nobody really likes Python the language especially much, but lots of people love Python the ecosystem, because it lets them get stuff done. I can think of a dozen languages I like better than Python, but none that's more practical in as many different contexts. -------------------- Edit: Apparently, there are some people who do like Python the language, and some people who dislike Python the ecosystem. If you want to…

in my experience, the python ecosystem is a mess. yes, there are lots of libraries, but that is just one part of the ecosystem. for one, everyone uses a different distribution, whether it's anaconda, python(x,y), or just the default python install. this creates a lot of fragmentation, and it's hard to come into a project and fix the mess that's been created. then there's the 2.x vs 3.x issue that is still a problem.…

You're right, it is a mess.

Don't get me wrong, there's a huge number of packages available and a lot of them are really quality and I have used the language a lot, but good gosh is the packaging a deployment a fucking disaster.

Environments are a 3rd party bolt on, that relies on hacking around with environment variables. There's no distinction between dev dependencies and actual, application dependencies. The packaging/deployment tool has gone through a bunch of different formats/styles and when it comes to deploying your code, you have nothing better than writing a bash script that automates your environment creation, activation and running of the packaging tool (you then just hope that Pip doesn't fail on some bizarre edge).

Re: Stop writing lambda expressions in Python

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

> of course you don't owe anyone anything

Yes, and thanks for acknowledging that. But then the phrase "owe it to yourself" didn't really communicate your intent correctly, which is why I objected to it.

Re: Stop writing lambda expressions in Python

#125
post #85

Earlier quoted context omitted.

Python has a convention for "private" functions, which is to prepend "_" to the name. > It's hard to make a strong case just in words, but once you really understand the power of the fat arrow syntax for anonymous inline functions then you use it more and more and it becomes second nature and the programs you write have a completely different style, oriented towards neatly positioned inline anonymous functions everyw…

Private functions do not restrict the usage of that function to one point in the code. There's a world of difference between a coding convention - essentially a comment to say "please don't access this function from outside the current class" versus it simply being impossible to do so. And the "private" convention does not say "don't use this function at any other point except one", it says "please don't access this…

As a user of JavaScript and es6, fat arrows are only necessary if you haven't started using async and await syntax, and I found myself almost never using fat arrows after moving to async/await.

Indeed naming my functions resulted in more testable and clearer code.

Re: Stop writing lambda expressions in Python

#127

Earlier quoted context omitted.

I suppose I like the readability too, but there are a number of things about the language I would rather had been done differently; I still insist that its approach to scope is worse than any choice except making everything global.

If the local/ global scooping rules are hurting you that much, you are probably putting more state in module-level variables than is healthy.

It's not that: it's the hobbled function scope that doesn't handle nested functions the way I would expect and hope.

Re: Stop writing lambda expressions in Python

#128

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…

Why does it need that, though? What's wrong with just naming the function?

It's not so much naming the function, but that doing so moves it outside the flow of the logic it is participating in. Especially when it's going to reference local context that is particularly confusing. There are definitely times when extracting out a function and naming it is the right thing to do. But there are an equal number of times when keeping all the local context together is better. It's probably hard to appreciate this unless you spend some time in languages where closures are seamlessly integrated like Groovy.

Re: Stop writing lambda expressions in Python

#129

Earlier quoted context omitted.

To give an example based on the article, I find it cumbersome to use itemgetter. I could use the library function: import operator sort(values, key=operator.itemgetter(2)) but it seems decidedly trivial and common to use a lambda: sort(values, key=lambda x: x[2])

The first returns a callable, the second a value.

The first returns a callable that's the value assigned to the key argument. The second is a callable that's the value assigned to the key argument. They're the same thing.

Re: Stop writing lambda expressions in Python

#130
The author uses this as an argument against lambdas:

> lambda expressions are an odd and unfamiliar syntax to many Python programmers

Well these python programmers now have an opportunity to learn this unfamiliar syntax. You don't avoid using parts of a programming language just because it's unfamiliar! To new Python programmers, a lot of the syntax is unfamiliar at first, especially if coming from a curly-brace language or new to programming in general.

While I like many of the ideas in the article (especially the use of standard library functions that already do what your lambda was trying to do), it seems to make some cases against using lambda where the justification isn't entirely valid. :/

Post reply on HN