1

I can't find if PEP 8 talks about vertical distance of dependent functions (this is covered in the Clean Code book, Chapter 5: Formatting, section Vertical Formatting > Vertical Distance) and I want to know what the Python rules are for the order of function definitions.

After reading the Clean Code book, I'll create a class that defines the methods in the same order as they are used, like this:

class A:
    def main(self):
        self._function_1()
        self._function_2()
        self._function_3()

    def _function_1(self):
        print("Hi 1!")

    def _function_2(self):
        print("Hi 2!")

    def _function_3(self):
        print("Hi 3!")

But maybe PEP 8 specifies something different, because I see some scripts set the definition of the last used method first. They modify the above example as:

class A:
    def main(self):
        self._function_1()
        self._function_2()
        self._function_3()

    def _function_3(self):
        print("Hi 3!")

    def _function_2(self):
        print("Hi 2!")

    def _function_1(self):
        print("Hi 1!")

I've also heard that in Python the order of functions varies depending on whether they are in a class or not.

If you can give me information about it, I will be very grateful.

Thanks!

EDIT. A brief clarification, as shown in the examples in the question, I refer to vertical distance as how close the definition of a method is to its use, with no other method definitions in between.

Molina
  • 51
  • 1
  • 7
  • 1
    I doubt there's any standard for the order of methods, except I'd expect `__init__` to be first. – Barmar Dec 08 '20 at 21:13
  • 2
    I just checked PEP-8, it doesn't say anything about the order of methods. I'm not sure what you mean by "vertical distance". – Barmar Dec 08 '20 at 21:15
  • 1
    Your question assumes that methods are only used from one place, otherwise the concept of "last used" makes no sense. But that's not likely to be common. – Barmar Dec 08 '20 at 21:15
  • Thanks Barman. I use "vertical distance" to follow the term used in the Clean Code book. It means being vertically close, without other code between them (as possible). The text of the book says: "Dependent Functions. If one function calls another, they should be vertically close, and the caller should be above the callee, if at all possible". – Molina Dec 08 '20 at 21:34
  • Again, that assumes that the functions are only used together. Most functions are intended for more general use, not just from a single dependent function. – Barmar Dec 08 '20 at 21:35
  • PEP 8 is agnostic about these considerations, AFAIK – juanpa.arrivillaga Dec 08 '20 at 22:23
  • There does not seem tooling available to enforce "vertical distance" or "step down rule". And if there is no tooling to enforce it, the order will probably be lost when working with multiple persons. If you are alone you can sort as you like (Allphabetical order is already provided by structure view of PyCharm). Also see related question: https://stackoverflow.com/questions/76611154/how-to-automatically-sort-functions-in-classes-by-their-usage – Stefan Jul 04 '23 at 10:57
  • In a plain python script, a function definition has to be located before its usage. Usually, there is an expression at the end of the file, jumping to the top of the file where a main function is located. With this "workaround" the functions can be put in wanted order, reading them from top to bottom, as in your first example. – Stefan Jul 04 '23 at 11:07

1 Answers1

0

Looks like in PEP 8 it's near the top of the style guide.

Blank Lines

Surround top-level function and class definitions with two blank lines.

Method definitions inside a class are surrounded by a single blank line.

Extra blank lines may be used (sparingly) to separate groups of related functions. Blank lines may be omitted between a bunch of related one-liners (e.g. a set of dummy implementations).

Use blank lines in functions, sparingly, to indicate logical sections.

Python accepts the control-L (i.e. ^L) form feed character as whitespace; many tools treat these characters as page separators, so you may use them to separate pages of related sections of your file. Note, some editors and web-based code viewers may not recognize control-L as a form feed and will show another glyph in its place.

https://peps.python.org/pep-0008/#blank-lines

  • Hi Jacob, thanks for your reply. I think you have confused *vertical distance* with blank lines, I've just updated the question with a brief clarification to avoid future misunderstandings. Despite that, your answer helped me to see that I add a blank line after the class names, I've just removed these unneeded blank lines. – Molina Aug 18 '22 at 05:35