Python Code Style Exploration(PEP 8)

During internship, I will focus on python programming. Let’s explore PEP 8 Style python code.

code is read much more often than it is written

Readability counts. Also, if applying the guideline make the code less readable, I will choose not follow it.

Absolute imports are recommended, explicit relative imports are acceptable.

If using relavtive import can avoid complex import layouts, it is acceptable. However, it also means we should design package layout more clearly.

If operators with different priorities are used, need to conly add whitespce around the lowest priority. Not every operator.

Usually, I just add a whitespace to every operator, will adjust my style after this.

Don’t use spaces around the = sign when used to indicate a
keyword argument, or when used to indicate a default value for an
unannotated function parameter.

Python code sometimes is likely to be compact.

When combining an argument annotation with a default value, however, do use > spaces around the = sign

Never use the characters ‘l’ (lowercase letter el), ‘O’ (uppercase
letter oh), or ‘I’ (uppercase letter eye) as single character variable
names.

Considering the number 0, 1, this is very reasonable

Use ''.startswith() and ''.endswith() instead of string
slicing to check for prefixes or suffixes.

When checking if an object is a string, keep in mind that it might
be a unicode string too! In Python 2, str and unicode have a
common base class

Need to take care of type of string.

There is still many rules that I do not very deep in. After using python for several month, this may be more clear to me.