University of Toronto function design recipe

Richard Vankoningsveld
1 min readNov 1, 2016

--

I’m doing this course, to transition to Python 3 mainly. But, I’m already discovering why it’s so well regarded, and that I have much to learn!

I really like there function design recipe, it goes like this:

  1. Write some examples of what you want your function to do:
    >>> some_function(arg)
    some_result
    >>> some_function(arg2)
    some_other_result
  2. Write the type contract:
    (parameter types) -> return type
    e.g. (number, number) ->int
  3. Write the function header, with meaningful names:
    def meaningful_func_name(meaningful_param_names):
  4. Write the description. Explain what is returned, and mention parameters explicitly.
  5. Write the body of the function (the actual code).
  6. Test using the examples written in point 1.

Points 1, 2 and 4 become the docstring for the function, which can be called using help(function_name). The final function might look something like this:

--

--