4

As you can see from the code below, I'm adding a series of functions to a list. The result is that each function gets ran and the returned value is added to the list.

foo_list = []
foo_list.append(bar.func1(100))
foo_list.append(bar.func2([7,7,7,9]))
foo_list.append(bar.func3(r'C:\Users\user\desktop\output'))

What I would like to know is, is it possible to have the function stored in the list and then ran when it is iterated upon in a for loop?

enter image description here

tisaconundrum
  • 2,156
  • 2
  • 22
  • 37

1 Answers1

4

Yeah just use lambda:

foo_list = []
foo_list.append(lambda: bar.func1(100))
foo_list.append(lambda: bar.func2([7,7,7,9]))
foo_list.append(lambda: bar.func3(r'C:\Users\user\desktop\output'))

for foo in foo_list:
    print(foo())
Philip Tzou
  • 5,926
  • 2
  • 18
  • 27