CS 116: Introduction to Computer Science 2

Python Style Guide

We expect you to follow the general style guidelines (PDF) discussed and developed in CS 115.

  • Helper functions only require a purpose and a contract unless other components are required in the question.
  • Make sure your variable and function names are reasonable and informative.
  • Use proper indentation to emphasize the logic of operations and nested constructs.
  • Take the time to specify the purpose and contracts for functions you create. For functions with side-effects, describe those as part of the purpose.
  • Give proper examples for your functions.
  • For your Python programs, we still want you to follow the contract style you learned for Scheme. Python (like Scheme) doesn't enforce contracts, but it's still essential that you make sure the arguments to your functions are of the type you say. We will be more tolerant of slight variation in contract form, but every function must be labeled as to what type of input it expects and what kind of output it produces.
  • As your functions grow, you will need to exercise judgment about whether your functions need or do not need examples. Functions that do not return values, for example, largely need helpful "purpose" headers, and examples can be descriptive of what happens, rather than giving the value of the function for a given input.
  • Python, as we shall see, mandates indentation, so you will be forced to indent your code usefully.
  • In general, you'll need to provide enough commenting in your code so that both you and other people can understand what it does. Programming is a form of communication, and not just between you and a computer.
  • For your testing, you must download the check module to be able to do testing similar to the format of check-expect in Scheme. To use this module, save check.py in the same directory as your assignment questions and write import check at the top of your file. For further information, refer to the Python Style Guide above.

Last modified on Friday, 19 July 2019, at 09:44.