In Python, all functions must be defined before they are available. This leaves two options for writing a script:
Define the functions above:
def f1():
def f2():
f1()
f2()
Define the functions below:
def main():
f1()
f2()
def f1():
def f2():
if __name__ == '__main__':
main()
Starting with Fortran, I was used to define functions at the bottom, but I saw a lot of Python scripts where the definition at the top seems to be preferred.
Is one of these options more Pythonic / recommended than the other? If so, why?
(I do not ask for an “opinion”; I ask for a reasoned reason why one of them is preferable, or if both of them are considered equally valid.)
source
share