[Tutor] Anonymous function
Alan Gauld
alan.gauld at yahoo.co.uk
Wed Mar 16 20:57:53 EDT 2022
On 17/03/2022 00:00, Devanshu Nalavade wrote:
> a user-defined function. This function has an anonymous function that takes
> the same arguments and does the summation. However for some reason the code
> is not giving me the desired output, there are no syntax errors so I'm not
> understanding what I'm doing wrong.
There are so many issues here it is hard to know where to start,
so my apologies if I over simplify things.
Caveat: My code below is untested so may not be 100% correct...
>>>> def sum(arg1,arg2,arg3=10):
> ... m = lambda arg1,arg2,arg3:arg1+arg2+arg3
That creates a function object(aka a lambda) and
assigns it the name m
It is identical to:
def sum(arg1,arg2,arg3=10):
def m(arg1,arg2,arg3):
return arg1+arg2+arg3
But sum() does nothing except define the function - which
is then just thrown away when sum() finishes.
And because sum has no return value Python returns None - the
default for functions.
But of course calling the function sum means you have hidden
the built-in function sum(), which does a similar thing but
for an unlimited number of args and without the default
value of 10 for arg3.
Hiding the built in functions is not usually a good idea.
Perhaps calling it sum3() or some-such would be better.
Also you use the names arg1,arg2,arg3 as the definition of the
parameters to lambda. But that creates 3 new names inside the
function, it does not pass the parameters of sum() into the
lambda as I suspect you think it does! It potentially then
becomes very confusing so I suggest using different names
in the lambda, say a1,a2,a3?
>>>> print(sum(5,6))
>
> The output comes out as 'None'
Yes because your function does not return anything, not
even the lambda.
To get this to work you'd need to modify it like so:
def sum3(arg1,arg2,arg3=10):
m = lambda a1,a2,a3:a1+a2+a3
return m(arg1,arg2,arg3)
print( sum3(5,6) ) # prints 21
That now defines the lambda and then calls it.
but since the lambda is not stored anywhere you might
as well just execute the code directly:
def sum3(arg1,arg2,arg3=10):
return arg1+arg2+arg3
Or even
def sum3(arg1,arg32,arg3=10):
return sum(arg1,arg2,arg3)
Normally we use lambdas when we want the return value
to be a function that the caller will use later. Or where
we want a function passed in and the caller provides a
lambda. We don't usually use them to define a function
that we immediately call then throw away.
For example here is a function that returns a function
that sums but with a user defined default value:
def sum3N(N):
return lambda a1,a2,a3=N: sum(a1,a2,a3)
sum10 = sum3N(10)
sum10(4,5) # returns 4+5+10 = 19
sum666 = sum3N(666)
sum666(10,20) # returns 696
sum666(1,2,3) # returns 6
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list