Python Forum
Proper Layout of Code Question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Proper Layout of Code Question
#1
Is there a 'standard' or 'preferred' way of coding in Python when coding functions?

Should the functions be listed in order they are called from top to bottom? Bottom to top? For example, is this ok or should fun1 be listed first because it's used first?

def fun1:
do something

def fun2:
do something

print('hello')
fun2()
fun1()

new to Python..be gentle! Big Grin
Reply
#2
In my experience there is no unwritten rule that states that you have to order in a certain way. But there are however some clever tricks to structure your code.
If you would start with object oriented Programming (OOP) you would categorize your methods by set-methods, get-methods, methods of a certain kind (calculation, visualization, ...), abstract-methods, static-methods, underscore-methods, ...
For imports it is useful to order the imports by project-external and project-internal imports and inside of this order alphabetically. And a similar ordering can be done for the functions. you could order them alphabetically in general, you could order them by their purpose (visualization, calculation, file operations, ...) and inside of each topic you could order them alphabetically.
But you could also order them by the order they are called, but I prefere the ways described above, it is way easier to find anything in there :D
There is only one rule that the computer itself is giving you. When we write functions we need to make sure that the function is declared before its usage outside of other functions:
def f1():
   pass

def f2():
    pass

f1()
f2()
is definitely fine.
def f1():
f2()

def f2():
pass

f1()
f2()
[/python]
is fine as well. But:
def f1():
pass

f1()
f2()


def f2():
pass
[/python]
will not be accepted by the interpreter, since the function f2 is called before it is defined.

So in a nutshell, I often see it as an individual choice, but that is how I do my coding. Maybe someone has a different experience :)
Reply
#3
Great! Thanks for the help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pyserial issues with proper loops and binary jttolleson 16 2,464 Nov-02-2023, 08:39 PM
Last Post: deanhystad
  Getting proper x,y axis values pyhill00 8 1,593 Jul-29-2022, 06:48 PM
Last Post: pyhill00
  Proper way to do the OR statement? Mark17 5 1,741 Mar-01-2022, 01:54 PM
Last Post: Mark17
  proper use of 'end' in a 'with' statement ccrider27 1 2,021 Mar-18-2020, 10:33 PM
Last Post: buran
  Proper use of if..elif..else statement nick1941 2 2,376 Mar-06-2020, 11:22 PM
Last Post: nick1941
  Unable to do the proper split using re.sub incase of missing data. Karz 1 1,829 Nov-17-2019, 05:58 PM
Last Post: buran
  getopt with tuple not working proper Frank123456 0 1,852 Aug-21-2019, 12:46 PM
Last Post: Frank123456
  proper syntax for itertuples? ilcaa72 1 1,970 Jun-06-2019, 02:41 AM
Last Post: scidam
  Need help excluding Named Entity (NE) and proper nouns (NNE) from text analysis disruptfwd8 0 2,324 May-15-2018, 12:10 AM
Last Post: disruptfwd8

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020