(Python) Can I store the functions themselves, but not their meaning, in a list

As you can see from the code below, I am adding a number of functions to the list. As a result, each function is launched, and the return 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 would I like to know if it is possible for a function to be stored in a list and then run when it is repeated in a for loop?

enter image description here

+4
source share
1 answer

Yes, 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())
+4
source

Source: https://habr.com/ru/post/1657431/


All Articles