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:
- Write some examples of what you want your function to do:
>>> some_function(arg) some_result >>> some_function(arg2) some_other_result
- Write the type contract:
(parameter types) -> return type # e.g. (number, number) ->int
- Write the function header, with meaningful names:
python def meaningful_func_name(meaningful_param_names):
- Write the description. Explain what is returned, and mention parameters explicitly.
- Write the body of the function (the actual code).
- 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: