5 ms·
I think I meant the treatment of anonymous functions - lambdas are limited compared to normal functions.
by thatswrong0 7y ago
I think I meant the treatment of anonymous functions - lambdas are limited compared to normal functions.
- im3w1l 7y agoIf you need more power than a lambda you would use a nested function, e.g. def a(n): def b(): print(n) return b
- saagarjha 7y agoThat's a lot of boilerplate for a closure…
- earenndil 7y agodef a(n): return lambda: print(n)
- saagarjha 7y agoThat's a couple characters over most languages I normally use. Plus lambdas have a number of strange warts and restrictions (no multi-line lambdas?) that leave me feeling that Python really dislikes traditional functional programming constructs.
- WoodTree 7y agoPython has become less functional over time. It’s also not a particularly good OO language either. It’s really a more descriptive sort of bash that became ubiquitous despite its short comings.
- mlyle 7y agoThe trivial lambda case is short, and you can do what the parent said if you want to do something that doesn't fit into the trivial lambda case well.
- saagarjha 7y agoThe "lambda" keyword isn't :(
- ptx 7y agoWhere's the boilerplate? "def b():" and "() => {}" are both 8 characters. It is annoying that you have to give the function a name, though.
- aserafini 7y agoOn the one hand it’s annoying, on the other it enforces a discipline of thought in the same way that having to name variables does.
- baddox 7y agoThat’s an interesting principle, something like “a block of code that is complex enough to be written across multiple lines of code ought to be given a name,” but standard idiomatic Python already violates that principle by featuring multiple lines in the body of a for loop. The fact that the body of a for loop can be multiple lines but the “body” of a lambda cannot is an arbitrary syntax inconsistency.
- jcheng 7y agodef b(x): return x + 1 vs x => x + 1
- WoodTree 7y agoNesting functions like this can have a bad performance impact because functions in Python are objects. Normally, all of those function objects are instantiated once when you load the module. However nested functions will be instantiated at runtime every time their parent is called, even if they aren’t used. This cost is perceptible in hot paths.
- edflsafoiewq 7y agoAre lambdas different?
- baron816 7y agoI’ve never seen it create a bottle neck, have you?
- weyland108 7y agoIt will definitely be a problem if you use JIT interpreter like pypy
- krallja 7y agoA lambda also behaves this way; it’s how it captures the closure scope.
- dwaltrip 7y agoThe lack of multi-line lamdas is one of my biggest gripes with python.
- whalesalad 7y agoIf you feel the need to use a multi line lambda you should be using a regular function. This is a code smell for sure.
- dwaltrip 7y agoRegular functions definitions can't be used as expressions. You are basically saying that one should never need a multi-line lambda. Obviously, I disagree.
- joshuamorton 7y agoIf you need to express something complex enough that you can't do it in a single statement in Python, it deserves to have a name (or more likely, it already has one in the standard library).
- baddox 7y agoBut python for loops can and typical do have multiple lines, yet that “block” inside the for loop doesn’t have a name. The fact that the body of a for loop can have multiple lines but an anonymous function cannot is a purely arbitrary syntax limitation, and the principle you stated ought to apply (or not apply) equally to both use cases.
- dwaltrip 7y agoThanks for writing this, that clarifies the issue more clearly and concisely than what I had in mind. I will mention a core aspect of my argument, to add to your point. When you make a named function and then use it separately somewhere else, there is a loss of locality. The logic is now further from its point of use. This is a cost, and sometimes it is an unreasonable one.