Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I'm trying to figure out Python lambdas. Is lambda one of those "interesting" language items that in real life should be forgotten?

I'm sure there are some edge cases where it might be needed, but given the obscurity of it, the potential of it being redefined in future releases (my assumption based on the various definitions of it) and the reduced coding clarity - should it be avoided?

This reminds me of overflowing (buffer overflow) of C types - pointing to the top variable and overloading to set the other field values. It feels like a sort of techie showmanship but maintenance coder nightmare.

1 Answer

0 votes
by (106k points)

Python lambda is another way or you can say a fancy way of saying function. Other than its name, there is nothing obscure, intimidating or cryptic about it. So when you read that following line, just replace lambda by function in your mind.

mult3 = filter(lambda x: x % 4 == 0, [1, 2, 3, 4, 5, 6, 7, 8, 9])

Output:- 4,8

So the above code with lambda just defines a function of x. Some other languages, like R, say it explicitly:

def filterfunc(x): 

     return x % 4 == 0

mult3 = filter(filterfunc, [1, 2, 3, 4, 5, 6, 7, 8, 9])

Output:- 4,8

So the above two code examples show us that the lambda function is more efficient because it replaces large functions in a minimum number of lines.

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...