Organizing your Python Code

In spite of yourself

Keno Leon

--

Original photo by Tara Shankar at Pexels

The Problem:

I am not an organized coder, or rather I struggle with organizing my code, nothing new, this has been going on for a long time…

📝 Many years ago I made a php image CMS of a few thousand lines… in one single file, most of the functionality was inside a giant loop ( plenty of nested loops, though), no functions, no comments, variables recklessly added here and there,  it was so bad it took me longer to read the code than to add functionality, in desperation I printed the whole thing, took it to the bar and drunkenly tried to make sense of it, it didn’t and at some point I gave up on it.

Ever since, organizing my code has been something I think one should strive for, mostly to avoid failure, and I try to, it makes life easier for you and whoever ends up reading and maintaining your code, let’s explore some aspects and solutions here; this is meant as a simple, beginners friendly overview, not a definite resource, the subject as you will see can get quite complex.

Some spaghetti code for starters:

for i in [1,2,3]:
def printMa():
print ('Ma')
x = True
if x == True:
printMa()
y = False
if y == True:
printMa()
else:
print("Ma")
y = True
if x…

--

--

Responses (5)