Python Forum
Using a lambda function within another function
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using a lambda function within another function
#1
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 (???)
Reply
#2
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.
Reply
#3
(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.
Reply
#4
I think editing post is allowed after 4 or 5 posts.
Reply
#5
(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?
Reply
#6
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.
Reply
#7
(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?
Reply
#8
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 >>>
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The function of double underscore back and front in a class function name? Pedroski55 9 644 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  Add two resultant fields from python lambda function klllmmm 4 905 Jun-06-2023, 05:11 PM
Last Post: rajeshgk
  Exit function from nested function based on user input Turtle 5 2,896 Oct-10-2021, 12:55 AM
Last Post: Turtle
Question Stopping a parent function from a nested function? wallgraffiti 1 3,669 May-02-2021, 12:21 PM
Last Post: Gribouillis
  Writing a lambda function that sorts dictionary GJG 1 2,013 Mar-09-2021, 06:44 PM
Last Post: buran
Question exiting the outer function from the inner function banidjamali 3 3,522 Feb-27-2021, 09:47 AM
Last Post: banidjamali
  Passing argument from top-level function to embedded function JaneTan 2 2,239 Oct-15-2020, 03:50 PM
Last Post: deanhystad
  use NULL as function parameter which is a pointer function? oyster 0 2,502 Jul-11-2020, 09:02 AM
Last Post: oyster
  How to use function result in another function Ayckinn 4 2,817 Jun-16-2020, 04:50 PM
Last Post: Ayckinn
  translating lambda in function fabs 1 2,148 Apr-28-2020, 05:18 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020