Live data from Hacker News

Stop writing lambda expressions in Python

treyhunner.com

101–110 of 202 posts

Re: Stop writing lambda expressions in Python

#101
post #86
post #13

> I’d say that using lambda expressions is acceptable only if your situation meets all four of these criteria: > 1. The operation you’re doing is trivial: the function doesn’t deserve a name Sure. > 2. Having a lambda expression makes your code more understandable than the function names you can think of This is just vague. Locally defining a function right where you need it makes code much more understandable to me…

Also, wouldn't the existence of a library function (#3) imply that what you're doing is either non-trivial or common enough that someone has already given the function a name? Seems like #1 is sufficient.

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])

Re: Stop writing lambda expressions in Python

#102
post #92

Earlier quoted context omitted.

>>> test(2) is None True

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

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.

Re: Stop writing lambda expressions in Python

#103

Earlier quoted context omitted.

I disagree... I came for the syntax and readability; I've stayed for the community.

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.

Re: Stop writing lambda expressions in Python

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

Replying to your reply of the child comment:

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

No, it's always been there.

    >>> def f():
    ...     pass
    ...
    >>> f() is None
    True

Re: Stop writing lambda expressions in Python

#105
post #99

Earlier quoted context omitted.

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.

i didn't choose python 2.7. it is what i have installed on my work computer because the three projects i have worked on at work that used python were all using python 2.7, and the projects were led by "python people". in a couple cases, it was me who got them to upgrade from 2.6 to at least 2.7, and i have tried communicating to people to use python3 if they insist on python. i wouldn't choose python for a new project unless i had to again help someone else's project.

python's version bifurcation problem is its problem, not mine. the problem exists.

Re: Stop writing lambda expressions in Python

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

It also doesn't have tail-recursion or macros. There are many who also regard that lack as a deep design flaw.

There never was a goal in Python development to support all programming paradigms.

How do you distinguish between "design flaw" and "different design goal than what you want"?

Re: Stop writing lambda expressions in Python

#108
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):…

As you've discovered, the reported property had a different name in python2. The repl doesn't report a property, it asks the object to give a representative string (__repr__() function) and prints that. Other languages wanting to expose something like this would maybe hide it deeper in the interpreter or build it into each tool separately, Python chooses to make everything accessible from the language itself.

You've bitten onto a more or less internal detail (I've TA'ed a bunch of undergrad courses in Python, and it's firmly in the land of things students don't know if they don't discover it on their own) and use that and seemingly quite little grasp of how Python works to argue that it isn't simple, while of course languages you are more familiar with are better.

Re: Stop writing lambda expressions in Python

#109

Earlier quoted context omitted.

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.

It also doesn't have tail-recursion or macros. There are many who also regard that lack as a deep design flaw. There never was a goal in Python development to support all programming paradigms. How do you distinguish between "design flaw" and "different design goal than what you want"?

I think, it was an example of intentional hostile design (say, see this video https://www.youtube.com/watch?v=NWZLB8CyPbM for what I mean). All, seemingly for the sake of that "only-one-way-to-do-things" goal.

EDIT: (Van Rossum's own explanation of why he thinks lambda's design is okay: https://www.artima.com/weblogs/viewpost.jsp?thread=147358 )

Re: Stop writing lambda expressions in Python

#110

I'm sorry but this article is mostly not Scottish. Lambdas in Python are an old non-issue. They are what they are. I like 'em because you can do "stupid python tricks" like Church numerals: https://en.wikipedia.org/wiki/Church_encoding plus = lambda m: lambda n : lambda f: lambda x: m(f)(n(f)(x)) succ = lambda n: lambda f: lambda x: f(n(f)(x)) mult = lambda m: lambda n: lambda f: m(n(f)) exp = lambda m: lambda n: n(m…

I tend to abuse lambdas plus itertools/functools to write "obfuscated" Python for fun. For example... * FizzBuzz with no control-flow keywords: https://github.com/ubernostrum/interviewer-hell/blob/master/... * Detecting if a number is a perfect square as a one-line lambda: https://github.com/ubernostrum/interviewer-hell/blob/master/...

Ah, those are great.

I can't find it now but some daredevil wrote a Lisp interpreter in a single Python expression. A work of art.

Post reply on HN