Python Programming Style Guide
Good programming style makes your code much more readable and
maintainable. Here are some basic style directives that I expect you
to follow in your code.
- Use empty space when appropriate. Separate related sections
of code from unrelated sections of code by the use of blank
lines.
- Comment sections of code that need it. You should
use comments to describe a particular twist that might be hard to
understand, or to explain what a section of code is doing. If your
program has no explanatory comments, you're assuming your readers
are more capable than they are.
- Break up lines that are longer than 80
characters. This makes your code much easier to read when printed
and when listed in a standard terminal window. In Python, you can
indicate that a line continues on to the next one by putting a
backslash (\) at the end.
- Use capitalization and descriptive names to make your
identifiers clear.
- Variable, function, and method, and file names should
start with a lower case letter. Capital letters or underscores
should be used to separate multiple words.
Examples: name, age, personName (or person_name), getAge (or
get_age). Whether you choose capital letters or underscores to
separate words, stay consistent.
- Class names should start with a capital letter. Capital letters
should be used to separate multiple words.
Examples: class Person, class Gradebook, class BlackjackGame
- Keep your functions and methods short. A
method shouldn't be longer than 1-2 screens without good reasons.
- Each file (called a module in Python) header should contain comments containing
- Name of module
- Author(s)
- Description of purpose
- Known bugs
- Each function and method should contain header comments containing
- Name of method
- Author(s)
- Description of purpose
- Description of parameters and return values
- Citations indicating ideas or code borrowed from
other people / sources
- Your code should be direct and minimal to get the job done. Don't use five variables and three loops when three variables and two loops will get the job done.
This content is a combination of commonly known programming style
guidelines, ideas that have been discussed here in our department,
and from Guido van
Rossum's Python
Style Guide.