Organizing your Python Code
By: Keno Leon
Tags: Coding, Programming, Software Development, Python, Data Science

Level 1: Functions and classes.

Both functions and classes are natural aggregators: Functions typically deal with statements (think actions or verbs and sentences) and classes with objects (think well, classes of things, or nouns and adjectives), programmatically they are deep subjects on their own and the cornerstones of the language, yet you could have a complex script or program run without them, so why use them ?

A well designed function will save you space and can be used as a sentence, building block or logic unit, a well designed class will dramatically expand your vocabulary , and together they will allow you to speak in paragraphs rather than yell commands willy nilly, let’s for instance rewrite the previous example with Functions:

def sing(line):

print ('Ma Ma')

if line == 1:

print('Mia')

else:

print ('Mia let me GO !') sing(1)

sing(1)

sing(2) # OUTPUT:

# Ma Ma Mia Ma Ma Mia Ma Ma Mia let me GO !

Classes ( in the words of the docs ) bundle data and functionality together , this allows you to start thinking in terms of more complex things, here for instance we can create a class that stands for a chorusSinger , sure there is more code to contend with, but we can now create unlimited chorus singers and ask them to sing the appropriate lines:

class chorusSinger:

"""This class creates a chorus singer that

can sing one of two lines.""" line1 = "Ma Ma Mia"

line2 = "let me GO !" def sing(self, line):

"""This function sings one of 2 lines"""

if line == 1:

print(self.line1)

else:

print(self.line2) # Create chorus singers:

chorusSinger1 = chorusSinger()

chorusSinger2 = chorusSinger() # Let them sing !:

chorusSinger1.sing(1)

chorusSinger2.sing(1)

chorusSinger1.sing(1)

chorusSinger2.sing(2) Output: Singer 1: Ma Ma Mia

Singer 2: Ma Ma Mia

Singer 1: Ma Ma Mia

Singer 2: let me GO !

Note that we also added comments in the form of “”” docstrings””" which can later be used to document your code and #inline comments , which help you re-read your own code.

We are not quite done with functions and classes, the execution if you notice this previous example is just left hanging there at the end, if you wanted to add sections or other singers it could get messy spaghetti, so we can further organize by adding a function that does that, once more what we earn is the power of multiplicity along with readability:

class chorusSinger:

"""This class creates a chorus singer that

can sing one of two lines.""" line1 = "Ma Ma Mia"

line2 = "let me GO !" def sing(self, line):

"""This function sings one of 2 lines"""

if line == 1:

print(self.line1)

else:

print(self.line2) def singPart():

"""Creates Singers and tells them to sing"""

chorusSinger1 = chorusSinger()

chorusSinger2 = chorusSinger()

chorusSinger1.sing(1)

chorusSinger2.sing(1)

chorusSinger1.sing(1)

chorusSinger2.sing(2) def main():

"""Plays parts"""

singPart()

singPart() # Runs the program

main() # OUTPUT:

Singer 1: Ma Ma Mia

Singer 2: Ma Ma Mia

Singer 1: Ma Ma Mia

Singer 2: let me GO ! Singer 1: Ma Ma Mia

Singer 2: Ma Ma Mia

Singer 1: Ma Ma Mia

Singer 2: let me GO !

This simple structure is fairly common, can serve as the basis of many short programs or scripts and open the door to more complex ones; here’s an overview of this pattern: