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


Messages In This Thread
Using a lambda function within another function - by JChapman - Jan-08-2019, 11:03 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  The function of double underscore back and front in a class function name? Pedroski55 9 940 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  Add two resultant fields from python lambda function klllmmm 4 1,052 Jun-06-2023, 05:11 PM
Last Post: rajeshgk
  Exit function from nested function based on user input Turtle 5 3,115 Oct-10-2021, 12:55 AM
Last Post: Turtle
Question Stopping a parent function from a nested function? wallgraffiti 1 3,799 May-02-2021, 12:21 PM
Last Post: Gribouillis
  Writing a lambda function that sorts dictionary GJG 1 2,083 Mar-09-2021, 06:44 PM
Last Post: buran
Question exiting the outer function from the inner function banidjamali 3 3,723 Feb-27-2021, 09:47 AM
Last Post: banidjamali
  Passing argument from top-level function to embedded function JaneTan 2 2,346 Oct-15-2020, 03:50 PM
Last Post: deanhystad
  use NULL as function parameter which is a pointer function? oyster 0 2,586 Jul-11-2020, 09:02 AM
Last Post: oyster
  How to use function result in another function Ayckinn 4 2,964 Jun-16-2020, 04:50 PM
Last Post: Ayckinn
  translating lambda in function fabs 1 2,225 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