I know that the order of functions in a script doesn't matter. However, this sample code doesn't work:
main.py
_FUNCTIONS = (_foo, _bar) def myfunc(): for f in _FUNCTIONS: print(f()) def _foo(): return False def _bar(): return True myfunc() provides the following error
File "main.py", line 1, in <module> _FUNCTIONS = (_foo, _bar) NameError: name '_foo' is not defined However, if I don't use _FUNCTIONS and inject (_foo, _bar) into a code this will work:
def myfunc(): for f in (_foo, _bar): print(f()) def _foo(): return False def _bar(): return True myfunc() Why the first example doesn't work?
How can I extract the list of functions in a variable and put it on the top of the script?
myfunc()directly after the definition ofmyfuncand it will fail like the first code snippet.defstatements will define a function, but not execute it. By the time you get to themyfunc()call, all the functions are defined. In your first example, you're using_fooand_barbefore theirdefs have been seen.