Please explain the logic of giving a name to lambda function when it is actually a nameless function?

0 votes
Why do we name lambda functions like in the below example:

a = lambda x,y: x*y
print(a(3,7))
Jun 12, 2019 in Python by Nisa
• 1,090 points
120 views

1 answer to this question.

0 votes

This is done because these functions are nameless and therefore require some name to be called. But, this fact might seem confusing as to why use such nameless functions when you need to actually assign some other name to call them? And of course, after assigning the name a to my function, it doesn't remain nameless anymore! Right?

It's a legitimate question, but the point is, this is not the right way of using these anonymous functions.

Anonymous functions are best used within other higher-order functions that either make use of some function as an argument or, return a function as the output.

For example:

def new_func(x): return(lambda y: x+y) 

t=new_func(3) 

u=new_func(2) 

print(t(3)) 

print(u(3)) 

OUTPUT:

6
5
As you can see, in the above example, the lambda function which is present within new_func is called whenever we make use of new_func(). Each time, we can pass separate values to the arguments.

They are usually used along with filter(), map() and reduce() functions since these functions take other functions as parameters.

answered Jun 12, 2019 by Wajiha
• 1,950 points

Related Questions In Python

0 votes
1 answer

Is it possible to run a function in Python using the command line?

Suppose your file name is demo.py and ...READ MORE

answered Jun 26, 2019 in Python by Neel
• 3,010 points
162 views
0 votes
1 answer

What is the Python logic to count the number of capital letters in a file?

Hi, You can use this piece of code, ...READ MORE

answered Jun 26 in Python by Roshni
• 4,530 points
72 views
0 votes
1 answer

What is the function to randomize the items of a list in-place?

Hello @Roshni, Python has a built-in module called ...READ MORE

answered Jun 26 in Python by Gitika
• 41,080 points
83 views
0 votes
1 answer

Using lambda functions to solve Algebra

Python lambda functions can be used to ...READ MORE

answered Jun 17, 2019 in Python by anonymous
340 views
0 votes
1 answer

Why are Python lambdas useful?

The main purpose of anonymous functions come ...READ MORE

answered Jun 19, 2019 in Python by Wajiha
• 1,950 points
87 views
0 votes
1 answer

What is the purpose of using lambda functions in Python?

The main purpose of anonymous functions come ...READ MORE

answered Jun 11, 2019 in Python by Nisa
• 1,090 points
179 views
0 votes
1 answer

What happens when you use lambda inside lambda?

Yes you can use lambda inside lambda. ...READ MORE

answered Jun 19, 2019 in Python by Wajiha
• 1,950 points
338 views
0 votes
1 answer

What is the use of join()?

Join()  function is used in threading to ...READ MORE

answered Jun 20, 2019 in Python by Wajiha
• 1,950 points
59 views
0 votes
1 answer

How to check if a website allows web scraping?

To check if a website allows web ...READ MORE

answered Jun 14, 2019 in Python by Wajiha
• 1,950 points
5,385 views