Python Forum
Multiple lambda functions in zipped list not executing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple lambda functions in zipped list not executing
#1
I want to execute multiple functions that have different arguments when a condition is satisfied, but I never get any of them executed with the correct arguments:

conditions = [condition1, condition2, condition3]
functions = [lambda: self._f(), lambda x: self._f2(a), lambda x,y: self._f3(b, c)]

for condition, function in zip(conditions, functions):
    if condition:
        function() # execute the proper function with 0, 1 or 2 arguments
        break

def _f():
    print('function1 with no arguments')

def _f2(x):
    print(f'function2 with one argument {x}')

def _f3(x, y):
    print(f'function2 with two arguments {x} and {y}')
But when the condition is satisfied, the corresponding function is executed with wrong parameters so it fails and raises an error. What I am doing wrong??

Thank you in advance!!

Ok, I just had to delete the lambda arguments and works fine:

From:

functions = [lambda: self._f(), lambda x: self._f2(a), lambda x,y: self._f3(b, c)]
to:
functions = [lambda: self._f(), lambda: self._f2(a), lambda: self._f3(b, c)]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code with empty list not executing adeana 9 3,726 Dec-11-2023, 08:27 AM
Last Post: buran
  list all functions in a package aka 5 1,742 Mar-16-2022, 07:38 AM
Last Post: aka
  List comprehension and Lambda cametan 2 2,234 Jun-08-2021, 08:29 AM
Last Post: cametan
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 2,316 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
  How to call multiple functions sequentially Mayo 2 9,330 Jan-06-2021, 07:37 PM
Last Post: Mayo
  Sorting list of names using a lambda (PyBite #5) Drone4four 2 2,725 Oct-16-2020, 07:30 PM
Last Post: ndc85430
  Defining multiple functions in the same def process sparkt 5 2,826 Aug-09-2020, 06:19 PM
Last Post: sparkt
  naming images adding to number within multiple functions Bmart6969 0 1,919 Oct-09-2019, 10:11 PM
Last Post: Bmart6969
  How to combine file names into a list from multiple directories? python_newbie09 3 5,206 Jul-09-2019, 07:38 PM
Last Post: python_newbie09
  Time multiple functions within functions Cortessizzz 4 3,114 Jan-09-2019, 04:15 PM
Last Post: Cortessizzz

Forum Jump:

User Panel Messages

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