Establishing your Python “Voice”

Henry Blais
4 min readFeb 20, 2019

--

All my practical posts to date have used Python 3 in one way or another. Python is a great, adaptive language, but like any tool, it’s only as effective as the person using it!

One great way to improve your coding abilities and really get the most out of Python (or any other language, really) is to establish or adopt a consistent, systematic “voice” that you use in all your future projects.

So What exactly is your Python “Voice” anyway?

Well, when you write code in Python, you will have to follow a general framework that the language can identify, but as you improve your code, you’ll learn that there are many different ways to achieve the same result. Some rules are unbending; for instance, Python will always require you to assign a variable before you can use it in practice.

From an organizational standpoint, however, some text-editing environments, like Jupyter Notebook, will allow you to run code in separate cells — which can create situations that disrupt your workflow and create a really unintelligible maze of disordered scripts! The following tips (ironically, in no particular order) can help you avoid confusion when you work with tools like Jupyter, and will ultimately result in neater, more readable code that you and your colleagues will appreciate!

1. Comment Your Code!

This is a basic tip, but it’s easy to overlook. In the course of building an application or a complex function, eventually you will find yourself in a real groove! Your code will come spilling easily from your fingers, your logic will be flawless, and you’ll write whole programs in one beautiful, elegant chunk.

And then, inevitably, you’ll be called away. Now, while the code you wrote may (or may not) be perfect in every way, I guarantee that at some point, you’ll return to a complex project you wrote this way, and be completely mystified by what exactly it does! If you don’t build up a habit of commenting your code inline, and labeling what each moving piece actually accomplishes, you will waste valuable time down the road reading through your scripts and trying to reconstruct exactly what you were thinking. Don’t trust the groove, comment that code.

2 . Design your Variable Assignment Thoughtfully

This is a little more nuanced, but bear with me. Often your scripts will depend on an input variable to pass values through the machinery of a function. In many cases, your functions will (and should) be designed to accept various inputs, and produce a consistent result in line with the intent of your function. Take the following code stub for example:

# Reminder to comment your code!!!# Assigning X prior to use, here, X will be a list:
X = [0,1,2,3]
# function that accepts a variable:
def ex_1(var):
# Assigning a variable INSIDE a function:
Y = [1,2,3,4]
# Variable of desired interaction between X and Y:
# assigns a list of common elements from X, Y:
Z = list(set(X) & set(Y))
# further operations on variable within function:
# operate on each item in Z:
for zed in Z:
# print elements of zed equally divisible by 2:
if zed % 2 == 0:
print(zed)

Thanks to my careful comments you already know what the cell above will do, but think about HOW and WHEN we assign X. If I assign X in the same cell as the function above, any time I run that cell, X will be re-assigned to the list [0,1,2,3]. But what if your work flow involves multiple, sequential transformations of X? If you ever carelessly run this cell again, any changes you’ve stored in local memory will by erased and re-assigned, and you’ll be stuck re running a bunch of inter-connected cells! This may sound a little obvious, but it’s a common rookie mistake that has obliterated stored work for a lot of people. Similarly, if your function writes a file to memory, be very careful about how that new file is named and assigned! You can overwrite a data file that is used by several notebooks in the blink of an eye, and waste even more of your time.

3 . Don’t depend too much on Local Memory

Python will take variable assignments and store them in local memory for you. In the course of your work, you may find yourself running test cases in throwaway-cells, before you run your actual code on your working dataset.

It is very easy to forget which cells are “core” and which are test cases, unless you maintain strict organization. This can result in situations where your script cells become disordered, and important variable assignments are out of place! Always operate on the assumption that your kernel could collapse at any moment, and keep every line of code in its proper order!

4 . Develop a Standard Vocabulary

You will find that Python is surprisingly flexible about spacing and some other syntactical nuances (even if that sounds crazy at first!). So, in cases like the function ex_1() above;

# This variable assignment
Z = list(set(X) & set(Y))
# Is completely identical to this one (spacing)
Z = list(set(X)&set(Y))
and this one (list syntax)
Z = [set(X) & set(Y)]

I recommend that you generally stick to one format unless you absolutely must alter the syntax for outside reasons. Not only will you learn to write your code much faster, but you will also avoid needless typos and errors from mistyping simple lines of code

5 . Learn and Use PEP8

PEP8 is an iconic, widely recognized style guide for Python. It’s a long read, but I recommend having a glance at it at least once. The guide is filled with guidelines for producing readable, neat scripts, and is really a piece of Python-Language history — a landmark of the internet, if you will. Down the line, you will discover that following the rules in PEP8 is both courteous and expected by colleagues and employers — deviating too much from this style guide is looked on as roughly equivalent to writing your professional resume in Comic Sans.

I hope this helps, and as always, thanks for reading!

--

--

Henry Blais

Financial tech analyst and programmer; I believe that transparency through Data Science is the defining next step in human progress this century.