Search code examples
pythoncoding-style

Order of functions within a Python source file


In languages such as C and C++, it is common to define called functions above their callers, in order to avoid the need for forward declarations. For example:

void g() { ... }

void f() { g(); ... }

int main() { f(); ... }

In Python, if the

if __name__ == '__main__': 
    main() 

idiom is used at the end of a source file, forward declarations are unnecessary and the order of functions does not matter.

Is there any convention for the order of functions within a Python source file? Are called functions still generally written above callers, or vice-versa?


Solution

  • There is no convention (for example, there is nothing in PEP 8) regarding function order. Even modules within the standard library are inconsistent.