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 in Python by Nisa
• 1,090 points
23 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 by Wajiha
• 1,120 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 in Python by Neel
• 1,530 points
30 views
0 votes
1 answer

Is there a foreach function in python and is there a way to implement it if there isnt any

Every occurence of "foreach" I've seen (PHP, ...READ MORE

answered Aug 31, 2018 in Python by charlie_brown
• 7,710 points
64 views
0 votes
1 answer

What is the recommended way to randomize a list of strings using Python?

Hi. Nice question. Here is the simplified answer ...READ MORE

answered Jan 18 in Python by Nymeria
• 3,500 points
38 views
0 votes
1 answer

Using lambda functions to solve Algebra

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

answered Jun 17 in Python by anonymous
13 views
0 votes
1 answer

Why are Python lambdas useful?

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

answered Jun 19 in Python by Wajiha
• 1,120 points
20 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 in Python by Nisa
• 1,090 points
26 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 in Python by Wajiha
• 1,120 points
11 views
0 votes
1 answer

What is the use of join()?

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

answered Jun 20 in Python by Wajiha
• 1,120 points
11 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 in Python by Wajiha
• 1,120 points
16 views