Why do we care about consistent code formatting:
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.
But most importantly: know when to be inconsistent – sometimes the style guide just doesn’t apply. When in doubt, use your best judgement. Look at other examples and decide what looks best. And don’t hesitate to ask!
Two good reasons to break a particular rule:
1) When applying the rule would make the code less readable, even for someone who is used to reading code that follows the rules. 2) To be consistent with surrounding code that also breaks it (maybe for historic reasons) – although this is also an opportunity to clean up someone else’s mess (in true XP style).
# Aligned with opening delimiter
foo = long_function_name(var_one, var_two,
var_three, var_four)
# More indentation included to distinguish this from the rest.
def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
import os
import sys
Do not import everything like this: from numpy import * (prevend name clashes)
In case this causes a name clash
from myclass import MyClass
from foo.bar.yourclass import YourClass
do this:
import myclass
import foo.bar.yourclass
In [ ]: