3

Is it this one?

def main():
    a()

def a():
    b()

def b():
    print("foo")

if __name__ == "__main__":
    main()

Or this one?

def b():
    print("foo")

def a():
    b()

def main():
    a()

if __name__ == "__main__":
    main()

They both work, but what is the more Pythonic or recommended way of ordering function definitions?

I feel like this is a silly and useless question (since they both achieve the same thing), but at the same time it bothers me that both choices seem equally sane and it's hard to pick one, and I'm wondering if there's a convention between one or the other in the Python community, or something in the PEPs that touches on this.

Deathkamp Drone
  • 282
  • 1
  • 5
  • 11
  • 2
    Possible duplicate of [What is a good way to order methods in a Python class?](https://stackoverflow.com/questions/10289461/what-is-a-good-way-to-order-methods-in-a-python-class) – AdamGold Jun 01 '19 at 13:52
  • Why does it bother you that there isn't One True Answer? – chepner Jun 01 '19 at 13:55
  • @chepner. You're straying into either psychology or religion, both of which are off topic for SO :) – Mad Physicist Jun 01 '19 at 13:57
  • I've already voted to close as primarily opinion-based :) – chepner Jun 01 '19 at 13:57
  • @chepner Convention and readability, mostly. If most Python programmers are doing it the first way, then I don't want to be the person that does it the second way. Most of the PEP8 is purely stylistic / readability rules so I thought this question was on-topic, at least for Python where these kinds of stylistic recommendations exist. – Deathkamp Drone Jun 01 '19 at 21:53

1 Answers1

0

I don’t think there’s anything discussing this subject in the docs. I would just say pick your preferred order and stick with it throughout your project. It’s mostly a question of readability - choose whichever suits you and the other developers working with you.

AdamGold
  • 4,941
  • 4
  • 29
  • 47