Python Forum

Full Version: Using a lambda function within another function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have very recently started looking at python, most of my (recent) prior programming experience being with VBA, and have been following the W3Schools tutorials. I am currently looking at Lambda functions (specifically here: https://www.w3schools.com/python/python_lambda.asp) and am struggling to implement them into my code. The following code works fine:

def foo(n):
  return lambda a : a * n

bar = myfunc(3)

print(bar(11))
However, the issues I come into arise when using a lambda function which uses a different number of arguments to the function that it is used within. For example, I cannot get the following code to work:

def foo(n,m):
  return lambda a : a * (n+m)

bar = myfunc(3)

print(bar(11,5))
What is the cause of this issue, and how should I resolve it?

Cheers Smile

(Jan-08-2019, 11:03 AM)JChapman Wrote: [ -> ]I have very recently started looking at python, most of my (recent) prior programming experience being with VBA, and have been following the W3Schools tutorials. I am currently looking at Lambda functions (specifically here: https://www.w3schools.com/python/python_lambda.asp) and am struggling to implement them into my code. The following code works fine:

def foo(n):
  return lambda a : a * n

bar = myfunc(3)

print(bar(11))
However, the issues I come into arise when using a lambda function which uses a different number of arguments to the function that it is used within. For example, I cannot get the following code to work:

def foo(n,m):
  return lambda a : a * (n+m)

bar = myfunc(3)

print(bar(11,5))
What is the cause of this issue, and how should I resolve it?

Cheers Smile

Edit: 'myfunc' should be 'foo'. Unfortunately I am not allowed to edit my post to correct this (???)
first code cannot run as myfunc has not been defined, to make it function use:
def foo(n):
  return lambda a : a * n
 
bar = foo(3)
 
print(bar(11))
Second code myfunc not defined, and passing only 1 argument where two are required.
(Jan-08-2019, 11:30 AM)Larz60+ Wrote: [ -> ]first code cannot run as myfunc has not been defined, to make it function use:
def foo(n):
  return lambda a : a * n
 
bar = foo(3)
 
print(bar(11))
Second code myfunc not defined, and passing only 1 argument where two are required.

Cheers for your response. However, I did pick up on the 'myfunc/foo' mistake in my previous post.
I think editing post is allowed after 4 or 5 posts.
(Jan-08-2019, 11:59 AM)Larz60+ Wrote: [ -> ]I think editing post is allowed after 4 or 5 posts.
Thanks for letting me know.

(Jan-08-2019, 11:30 AM)Larz60+ Wrote: [ -> ]passing only 1 argument where two are required.
So are you saying that when implementing a lambda function within another function, the 2 should always have the same number of arguments?
Sort of, the number of function arguments must be matched, unless optional. For example:
def foo(a, b):
    ...
must be passed two arguments.
but:
def foo(a, b=1):
    ...
Only requires a, but b is optional, and defaults to 1 if not provided.
(Jan-08-2019, 12:21 PM)Larz60+ Wrote: [ -> ]Sort of, the number of function arguments must be matched, unless optional. For example:
def foo(a, b):
    ...
must be passed two arguments.
but:
def foo(a, b=1):
    ...
Only requires a, but b is optional, and defaults to 1 if not provided.

Perhaps my entire interpretation of Lambda functions (when used within another function) is completely off. I perceived them as being similar to models and factories? That is, they allow you to create a 'blueprint' for a function from which new functions (which perform the same sort of operations) may be quickly and easily created?
I think your confusion is not with lambda, but with functions altogether. Let's look at your second snippet (fixed for the NameError) and with some additions from me

def foo(n,m):
    return lambda a: a * (n + m)
 
bar = foo(n=11, m=5)
 
print(bar(a=3))
on line#4 you supply values for n and m (i.e. arguments for foo() function, not the lambda. Now bar function is effectively the same as
def bar(a):
    return a * 16
and then you call bar(), supplying the a argument.

Now, what you describe as blueprint for function:
def my_power(power):
    return lambda x: x ** power
    
pow2 =  my_power(2) # pow2 will get an argument and will raise to power of 2
pow3 = my_power(3) # pow3 will get an argument and will raise to power of 3

print(pow2(2)) # x=2
print(pow3(5)) # x=5
Output:
4 125 >>>
Now there are other way to create "blueprints for function"
from functools import partial

def my_power(num, power):
    return num ** power
    
pow2 =  partial(my_power, power=2)
pow3 = partial(my_power, power=3)

print(pow2(2)) # num=2
print(pow3(5)) # num=5
# and you still can do
print(my_power(2, 3))
Output:
4 125 8 >>>
(Jan-08-2019, 01:22 PM)buran Wrote: [ -> ]I think your confusion is not with lambda, but with functions altogether. Let's look at your second snippet (fixed for the NameError) and with some additions from me
def foo(n,m): return lambda a: a * (n + m) bar = foo(n=11, m=5) print(bar(a=3))
on line#4 you supply values for n and m (i.e. arguments for foo() function, not the lambda. Now bar function is effectively the same as
def bar(a): return a * 16
and then you call bar(), supplying the a argument. Now, what you describe as blueprint for function:
def my_power(power): return lambda x: x ** power pow2 = my_power(2) # pow2 will get an argument and will raise to power of 2 pow3 = my_power(3) # pow3 will get an argument and will raise to power of 3 print(pow2(2)) # x=2 print(pow3(5)) # x=5
Output:
4 125 >>>
Now there are other way to create "blueprints for function"
from functools import partial def my_power(num, power): return num ** power pow2 = partial(my_power, power=2) pow3 = partial(my_power, power=3) print(pow2(2)) # num=2 print(pow3(5)) # num=5 # and you still can do print(my_power(2, 3))
Output:
4 125 8 >>>

That's great - cheers for the in-depth explanation.