Welcome to the CS410 Python Style Guide. Our mission in this course is to help you uplevel your technical skills while mastering the art of writing clean, consistent code. Trust us, your future self and teammates will thank you!
Why is this important? Clean code isn't just a nice-to-have; it's essential for effective collaboration, maintenance, and readability. This guide will walk you through industry standards and prepare you for the professional world.
Make sure to read through it carefully. Weβll be keeping an eye on your coding style, and repeated issues will lead to point deductions on your homework. But don't worryβwe're here to help you build great habits that will benefit you not just in this course, but throughout your entire coding career.
This style guide is built on the best practices outlined in PEP 8 Style Guide for Python Code and PEP 257 Docstring Conventions, the go-to industry standards for writing readable and consistent Python code. PEP 8, created in 2001 by Guido van Rossum, Barry Warsaw, and Alyssa Coghlan, aims to standardize Python code for clarity and uniformity. PEP 257 enhances documentation through clear and consistent docstring conventions. As the PEPs keep evolving with the language, you can be sure they will stay relevant and useful. Stick to these guidelines for happier coding!
user_name
, calculate_total
.mymod
.Ocean
, TreeHouse
.BadDataError
.MY_PYTHON_CONSTANT
.my_variable
, exciting_new_method
.In Python, indentation is a must. Your code will not compile or run properly if you donβt have the correct indentation. It can be a little finicky, though: if you use spaces to indent in some places and tabs in another your compiler will not be happy.
In the bottom right of your window in VSCode, thereβs a portion that will say βSpaces: <number>β or βTab Size: <number>.β Click on that and choose βIndent using spacesβ followed by β4β to have the correct settings. When youβre done you should see βSpaces: 4β in the bottom corner of your code window.
80 characters max: Lines of code should not exceed 80 characters.
Operator on new line: When wrapping lines, the operator should start the new line. For example:
Backslash for wrapping: Use a backslash (\
) at the end of a line to indicate continuation. Here's how it may come in handy:
Note: When wrapping with the backslash, the wrapped line of code must be indented so that the first character of the wrapped line is in line with the beginning of the expression on the original line (in this case, the equality operator).
The general principle of commenting is to clarify / explain your code to an outside observer (or to your future self!).
#
: Begin all implementation comments with #
.
Also known as a βdocstring,β documentation comments in Python appear directly underneath the declaration of each function.
Write docstrings for all functions including helper and nested functions. A good docstring should clearly describe the function's inputs, outputs, and purpose, allowing users to understand the function without needing to look at the body.
Example docstring:
self
Keywordself
: Always use self
to access methods and fields within a class. It should be the first parameter of any method in the class.