r/learnpython • • Jan 23 '22

I still don't understand the benefit of lambda functions

Hi all,

I've read about lambda functions (for python and other languages), I can WRITE lambda functions just fine, but it seems to me that most places I use them they're just...functions with different syntax. I mean they take args, they return values sometimes. What am I missing here? If anyone has examples of problems they've solved with lambda functions that would also be very helpful.

Thanks!

324 Upvotes

120 comments sorted by

View all comments

Show parent comments

1

u/siddsp Mar 28 '22

Which is why I said you can simply convert an attribute to a string after having it autocompleted by your IDE, making your point invalid.

0

u/[deleted] Mar 28 '22

[deleted]

1

u/siddsp Mar 28 '22

Because you're overly paranoid and don't even know what you're programming.

Not to mention that lambda is far worse when you want to access multiple attributes, and does not work anyway if you want the nature of attribute access to be dynamic -- At least not without ultimately being a ridiculously ugly "solution".

0

u/[deleted] Mar 28 '22

[deleted]

1

u/siddsp Mar 28 '22

Right, static analysis and autocomplete are for pussies!

No, you just happen to be too dense to understand the point.

Can you give an example?

Let's say I want to access multiple attributes and return the result as a tuple, but the attributes I want to access are at runtime (e.g. user input), with attrgetter, I can easily unpack the arguments as strings into the function call, and the result will return the tuple of values, while with a lambda, doing the same would be super ugly.

0

u/[deleted] Mar 28 '22

[deleted]

1

u/siddsp Mar 28 '22 edited Mar 28 '22

It's funny you take this as a personal attack, so much that you have to resort to insults.

I'm resorting to insults because you literally are parroting the same thing over and over again, which is a useless point that doesn't make a difference in this context. You don't actually refute anything and just keep repeating "static analysis is for pussies".

I guess it's fine though since reddit is the perfect place to be smug about it.

Either you're dense, or you're obviously are trying to irritate others with "concerns" that nobody actually cares about.

Oh god.

Yeah, except it's true. Try accessing multiple attributes with a lambda and claiming it's readable within one call. Go ahead. Try using a lambda with user input in a way that's readable. You simply can't.

But sure, you won't spend time refuting the point because you're only here to make non-points and complain about something that really doesn't matter.

Edit: You also just happen to be straight up wrong because lambdas don't have type hinting, so there you go.

0

u/[deleted] Mar 28 '22

[deleted]

1

u/siddsp Mar 28 '22 edited Mar 28 '22

The point is that with attrgetter, you won't get autocomplete or static analysis when accessing attributes, and with a lambda you will. This is not useless, it's a completely valid point.

Oof. They do.

No they don't. Show me where the type hint is (hint, there isn't one because there is absolutely no way to infer the type of x given that you can put any type hint for it):

lambda x: x.some_attribute

Lambda statements don't support them, so you're just flat out wrong. In literally every way. There is 0 legitimacy to your already stupid point.

0

u/[deleted] Mar 28 '22

[deleted]

→ More replies (0)