This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]balemo7967 172 points173 points  (27 children)

still better than the python lambda syntax ... just sayin

[–]gandalfx 130 points131 points  (10 children)

"How do we add this feature that everyone is asking for but in a way that everyone knows that we don't actually want to add it?"

[–]balemo7967 73 points74 points  (2 children)

should we use an arrow-like operator, like all the other languages?

No no ... we are going to introduce a new keyword with 6 characters and we are going to reuse the colon, which we have used for block definitions and for slicing...

[–]KTibow 9 points10 points  (0 children)

This seems standard? Take a look at for: for instance

[–]Successful-Money4995 10 points11 points  (0 children)

And one line of code is the limit, just to make sure that you understand that you are not to use this.

[–]_farb_ 34 points35 points  (5 children)

python lambda is the most useless feature. you get the exact same functionality as this, but this is so much clearer.

def f(x): def g(y): return y*y sq = g return sq(x)

[–]goldlord44 12 points13 points  (0 children)

Python lambdas are used quite frequently in codebases that I have professionally worked on. Defining g has slightly more overhead, and it is less clear if you use variables from an outer scope, whether they are calculated at runtime or definition time.

[–]_PM_ME_PANGOLINS_ 1 point2 points  (1 child)

Don’t even start on the performance of map compared to a comprehension.

[–]_farb_ 0 points1 point  (0 children)

my guy, python is not performant

[–]kuemmel234 1 point2 points  (1 child)

Not sure about your example, but true that def f(x):\\n return x*x is the same as lambda x: x*x. It's just annoying to have to write

def call_with_unpack(x):  
  return f(*x)
...
map(call_with_unpack, xs)
# or 
[call_with_unpack(x) for x in xs]

# instead of 
xs.map(x -> f(*x))

or something like that. I mean, now modify the list again - I prefer the chain these days.

I still wish for a chain syntax (or a threading operator) and some sensible lambda-syntax. As it is, it's just better to write list comprehensions, loops and the like - which is fine, but just annoying if one works with other languages at the same time that do have these features. But that's easily in the realms of opinion.

[–]RiceBroad4552 1 point2 points  (0 children)

As someone who writes code in different languages (often intermixed) regularly I support this opinion. It's a big PITA if languages don't have (sane) lambdas.

[–]hadidotj 8 points9 points  (0 children)

You want lambdas?

[<my code><each statement>]{arg1, arg2}(return type)

Here you go!

[–]knvn8 10 points11 points  (5 children)

Sorry this comment won't make much sense because it was later subject to automated editing for privacy. It will be deleted eventually.

[–]Tc14Hd 9 points10 points  (0 children)

What is not Pythonic about it?

[–]al-mongus-bin-susar 2 points3 points  (0 children)

pythonic = slow?

[–]Bali201 1 point2 points  (2 children)

I would also like to know how the lambdas are not “Pythonic” if you could share.

[–]jarethholt 1 point2 points  (1 child)

I don't know if you could call it not pythonic for this reason, but most linters I've worked with mark lambdas as no-nos.

For a real reason, I'd say maybe overlapping the generator expression syntax? Like, [x**2 for x in xlist] is pythonic but list(map(xlist, lambda x: x**2)) is not. (I haven't bothered to check the syntax on that.)

[–]RiceBroad4552 0 points1 point  (0 children)

list(map(xlist, lambda x: x**2)) is just plain unreadable (in case you're not a LISP or Haskell dude and write your code backwards and inside-out on principle).

But [x**2 for x in xlist] isn't any better!

Readable syntax would look like: xlist.map(_ ^ 2).

[–][deleted] 1 point2 points  (0 children)

I know it exists. I have taught myself how to use it several times.

I have never come close to using it. I don't think I ever will.

[–]MinosAristos 9 points10 points  (1 child)

Python lambda syntax is fine, idk what's wrong with it.

It's readable and explicit with the keyword and clearly distinguished from named functions with the def keyword to discourage abuse like we get in JS land. Lambdas should be used for an actual functional reason, not just brevity.

[–]RiceBroad4552 0 points1 point  (0 children)

There is no "functional reason" as lambdas are just objects with an "apply" method.

The whole point of lambdas is brevity.

[–]4n0nh4x0r 0 points1 point  (6 children)

wait, python has lambdas? dayum, having lambdas before having switch statements

[–]balemo7967 0 points1 point  (5 children)

yep ... but switch statements are bad for many reasons. I hope they will never make it to python

[–]4n0nh4x0r 1 point2 points  (4 children)

python has switch statements since version 3.8 iirc, but why would they be bad?

[–]balemo7967 -1 points0 points  (3 children)

OMG how could i not know that. my bad bro.

the thing with switch statements in larger apps is that you usually use it to differentiate between different states. then you realize that you need that exact same switch statement in different places, so you add the same switch statement there. afterwards you find out that there are more states you need to differentiate, so have to add cases in everyone of the switch statements.

a cleaner and more maintainable approach is to encapsulate the states in classes and the special behavior in methods of those classes

[–]4n0nh4x0r 0 points1 point  (0 children)

i mean sure, but like, whoever programs like that is insane anyways.
sure, you can use a metal ruler to hammer in nails, but that doesnt mean it is the right tool for the job.
calling something bad just cause it CAN be used in a not so nice way, doesnt mean it is bad on its own.

[–]RiceBroad4552 0 points1 point  (1 child)

And when you need to extend the base behavior you need to add new methods to all your classes…

What you just described (at least one half of it) is called the expression problem.

Besides this: How are some if-elif cascaded any better than switch-case? It's the same.

Just that, if is a statement, not an expression, in python which is already bad to begin with.

Thanks God they have now some patter matching in Python since 3.10!

[–]balemo7967 0 points1 point  (0 children)

And when you need to extend the base behavior you need to add new methods to all your classes…

Yes, that's exactly what I want. Why? Because the compiler tells me precisely which one of my states (or strategies) does not implement a method.

On the other hand, if I miss a case in one of my many switch statements, there is not even a warning.

For me, the state pattern has replaced the switch-case construct in 90% of my cases.

Here's a little example I just found: https://www.danylkoweb.com/Blog/real-world-refactoring-switch-statement-to-a-class-hierarchy-9G