Every programming language has its own guidelines and best practices. The official style guide in Python is defined in PEP 8. Just look at this example - which one is easier to read?
But the full style guide is very long, and you don't need to read/follow all of its rules. In this tutorial, I will list only the ones that I feel are most common and practical.
Blank Lines Between Functions
Letting your code breathe is one of the biggest improvements you can have. Just look at this code:
def get_input(file, column, parse=True): df = pandas.read_csv(file) # ... return df[column] def work_with_line(line): line['total'] = line['total'] * 0.21 # ... def format_output(input, output_file="output.csv"): for line in input.iteraterows(): line = work_with_line(line) # ... df.to_csv(output_file) return True
This code feels like a wall of text. It's hard to spot where one function ends and another begins. Even our IDE says that we need more space:
So let's do just that:
def get_input(file, column, parse=True): df = pandas.read_csv(file) # ... return df[column] def work_with_line(line): line['total'] = line['total'] * 0.21 # ... def format_output(input, output_file="output.csv"): for line in input.iteraterows(): line = work_with_line(line) # ... df.to_csv(output_file) return True
Now, it's much easier to see individual functions. This is a simple change that can make a big difference in readability.
Imports at the Top
The following example is about imports. You might have learned that you can import modules anywhere in your code:
def get_input(file, column, parse=True): import pandas df = pandas.read_csv(file) # ... return df[column]
But if we look at the PEP 8, it says that they should be at the top. Why is what? Well, it's easier to see what modules are being used in the code:
import pandas def get_input(file, column, parse=True): df = pandas.read_csv(file) # ... return df[column]
This lets you quickly see what modules are used in the code. It also makes it easier to see if a module is used in multiple functions.
Bonus: Having imports at the top also makes it easier to see if you are using a module you don't need or missing one!
Importing Different Modules
On the topic of import, you might have seen people importing multiple modules in one line:
import pandas, numpy, matplotlib # ...
And while this is valid Python code, it's not recommended by PEP 8. Instead, it would be best if you imported each module on a separate line:
import pandasimport numpyimport matplotlib # ...
This, once again, makes it easier to see what modules are being used in the code. But if you are importing many methods from a module, you can do it in one line:
from pandas import read_csv, to_csv
Note: Editors will not treat this as a violation of PEP 8
Spacing in Function Parameters
Another thing that PEP 8 recommends is to not use spaces around parameter inputs:
def get_input( file , column , parse = True): df = pandas.read_csv(file) # ... return df[column]
This, again, is an entirely valid code, yet it adds unnecessary clutter. Instead, you should remove the spaces:
def get_input(file, column, parse=True): df = pandas.read_csv(file) # ... return df[column]
Note: Editors will not treat this as a violation of PEP 8, but inconsistent spacing can make your code harder to read.
Naming Conventions
PEP 8 also has guidelines for naming variables, classes, and functions. For example, variables should be in lowercase, with words separated by underscores:
def get_input(file, column, parse=True): df_list = pandas.read_csv(file) # Recommended variable name dfList = pandas.read_csv(file) # Not recommended variable name # ... return df_list[column]
Classes should be in CamelCase:
class MyNewClass:# ...
And functions should be in lowercase, with words separated by underscores:
def work_with_line(line): line['total'] = line['total'] * 0.21 # ...
This makes it easier to see what type of object you are working with. It also makes it easier to see what a class is, what a function is, and what a variable is.
Tabs VS Spaces - the Old Battle
This one is here because it's a never-ending battle:
PEP 8 recommends using spaces instead of tabs due to tabs being interpreted differently by different editors.
The only crucial thing is to be consistent. If you are using tabs, use them everywhere. If you are using spaces, use them everywhere. Mixing them is not supported in Python and can lead to errors.
Avoid True/False in Comparisons
Another thing that PEP 8 recommends is to not compare to True
or False
directly:
if x == True:# ...
Instead, it would help if you used the fact that Python treats True
as 1
and False
as 0
:
if x:# ...
This is also a suggested practice by Editors:
Of course, there are exceptions to this rule, but in general, you should avoid comparing to True
or False
directly and use the fact that Python treats them as 1
and 0
.
Code Style is Just a Guideline, not Religion!
Even if PEP 8 is the official style guide for Python, it's a flexible rule set. It's a guideline. So treat this as a guide to better code, not a strict rule set. So don't apply PEP 8 if it breaks your application or makes it harder for you to work on it. Just remember to stay consistent, and you will be fine.
One of Guido’s key insights is that code is read much more often than it is written. The guidelines provided here are intended to improve the readability of code and make it consistent across the wide spectrum of Python code. As PEP 20 says, “Readability counts”.
A style guide is about consistency. Consistency with this style guide is important. Consistency within a project is more important. Consistency within one module or function is the most important.
However, know when to be inconsistent – sometimes style guide recommendations just aren’t applicable. When in doubt, use your best judgment. Look at other examples and decide what looks best. And don’t hesitate to ask!
In particular: do not break backwards compatibility just to comply with this PEP!
This was taken from official PEP 8 page
No comments or questions yet...