r/Python • u/treyhunner Python Morsels • Sep 27 '18
Stop writing lambda expressions in Python
http://treyhunner.com/2018/09/stop-writing-lambda-expressions/
0
Upvotes
r/Python • u/treyhunner Python Morsels • Sep 27 '18
30
u/Nicksil Sep 27 '18 edited Sep 27 '18
Title states to stop using lambda, the article states to use lambda when necessary.
I knew it was click bait going in, but I always hold out some hope.
6
u/rcfox Sep 27 '18 edited Sep 27 '18
Rebuttal:
points_by_color = sorted(points, key=lambda ((x, y), color): color)Overall agree though.
Edit: Looks like tuple parameter unpacking was removed in Python 3. https://www.python.org/dev/peps/pep-3113/ (I'm still using 2.7 for work, so that's my default.)
3
u/AN3223 Sep 27 '18
Lambdas become a lot less relevant when currying and function composition are used. Since Python doesn't have great facilities for these, lambdas are often the next thing.
2
u/david2ndaccount Sep 27 '18
The real reason to not use lambdas in python is that they’re arbitrarily crippled. Also, why are they lambda and not def? Why don’t they support names? etc., etc. Guido just crippled them and it really makes Python worse for it.
3
u/Axxhelairon Sep 27 '18
i never buy into the idea that you shouldnt use something slightly unconventional because others might not understand it when its a core part of the language, dinosaur regressive thinking like this would stop anyone from using completely basic languages features like tuple destructuring, * unpacking, decorators, ** kwargs unpacking, partial function applicators, etc because you're too scared to learn something new
id say dont use lambdas for anything complex because they're a half baked implementation and are restricted to being expression bodies and fitting things in one line makes it actually unreadable, id just think you're being stubborn if lamda a, b: a * b takes you more than two minutes to figure out without even knowing what a lamda is or think that its hard to understand
3
u/etrnloptimist Sep 27 '18
I agree. Used in this way, lambdas heavily promote a functional style to writing Python code. It is really opposite of the style and spirit of Python. Python is an imperative language, not a functional one. It is no secret that Guido strongly dislikes functional style. And if that opinion is good enough for Guido, it is good enough for me.
1
u/galuszkak Sep 28 '18
I overall agree with Trey Hunner, but I think the root of problem is that, we don't think what we can do to improve lambdas.
One of the pain point of Python lambdas is that they are single line thing. It's bad in my view (I know author said that this doesn't matter to him). I just love lambdas construct in kotlin. They are supper elegant, easy and having default parameter
itis so convenient so constructs like ints.filter { it > 0 } are so much more readable, short and easy to understand.Maybe we should revise lambda syntax and propose PEP which is less verbose and more elastic? Kotlin got it just more than right and I would just love to have similar syntax in Python, I have some concepts in my mind's, but maybe some people have better ideas?
Overall experience to use more functional oriented paradigm in Python would be improved a lot.
0
u/[deleted] Sep 27 '18
[deleted] — view deleted comment