200 likes | 362 Views
Day 2 – Lesson 5 Function Interfaces. Python Mini-Course University of Oklahoma Department of Psychology. Questions from Day 1. Installation Python syntax Variables and data types Creating and using functions Using the math library. Lesson objectives.
E N D
Day 2 – Lesson 5Function Interfaces Python Mini-Course University of Oklahoma Department of Psychology Python Mini-Course: Day 2 - Lesson 5
Questions from Day 1 • Installation • Python syntax • Variables and data types • Creating and using functions • Using the math library Python Mini-Course: Day 2 - Lesson 5
Lesson objectives • State the principles of good function interface design • Define encapsulation and state why it is useful • Use parameters to make functions generalizable • Use docstrings to document function interfaces Python Mini-Course: Day 2 - Lesson 5
Designing function interfaces • Interface • A summary of how the function is used • Documentation specifies: • What the function does • Parameters to be passed into the function • What (if anything) the function returns Python Mini-Course: Day 2 - Lesson 5
Designing function interfaces • Design principles: • KISS • Encapsulate • Make it as general as you can (keeping in mind principle #1) • “An interface is ‘clean’ if it is ‘as simple as possible, but not simpler.’” • Document!!! Python Mini-Course: Day 2 - Lesson 5
Encapsulation • “The hiding of the internal mechanisms and data structures of a software component behind a defined interface, in such a way that users of the component (other pieces of software) only need to know what the component does, and cannot make themselves dependent on the details of how it does it.”Wikipedia, http://en.wikipedia.org/wiki/Encapsulation_(computer_science) Python Mini-Course: Day 2 - Lesson 5
Example • Driving a car • You need to know how to use: • Gas and brake petals • Steering wheel • Gear shifter • You don’t need to know: • The kind of engine • How the engine (or any other system) works Python Mini-Course: Day 2 - Lesson 5
Why encapsulate? • Makes it easier to change the internal mechanisms • Protects the integrity of the component • prevents users from setting the internal data of the component into an invalid or inconsistent state • Reduces system complexity Python Mini-Course: Day 2 - Lesson 5
Code example def perimeter(length): p = length + length + length \ + length print p perimeter(3) Python Mini-Course: Day 2 - Lesson 5
Code example def perimeter(length): p = 4*length print p perimeter(3) Python Mini-Course: Day 2 - Lesson 5
Code example def perimeter(length, n_sides): p = length*n_sides print p perimeter(3, 4) perimeter(3, 6) Python Mini-Course: Day 2 - Lesson 5
Designing for generalizability • Try to think of all the possible uses for this function • Avoid hard-coding • Use parameters instead Python Mini-Course: Day 2 - Lesson 5
Parameters and arguments • Parameters are the values that a function receives as input def perimeter(length, n_sides): • You can also use parameters that specify a default value def perimeter(length, n_sides=4): Python Mini-Course: Day 2 - Lesson 5
Parameters and arguments • When you call a function, you pass specific values called arguments for each parameter perimeter(3) perimeter(3, 6) Perimeter(3, n_sides=6) perimeter(length=3, n_sides=6) Python Mini-Course: Day 2 - Lesson 5
Using docstrings • A string literal that occurs as the first statement in a module, function, class, or method definition • becomes the __doc__ special attribute of that object • See PEP-257 for detailshttp://www.python.org/dev/peps/pep-0257/ Python Mini-Course: Day 2 - Lesson 5
One-line docstrings • For really obvious cases def perimeter (length): """Calculate the perimeter of a square.""" • Use triple quotes even though the string fits on one line • Closing quotes are on the same line as the opening quotes • No blank line either before or after the docstring Python Mini-Course: Day 2 - Lesson 5
One-line docstrings • Should be a phrase ending in a period • Prescribes the function or method's effect as a command, not as a description • """Do this.""" or """Return that.""" • NOT """Returns the pathname ...""" Python Mini-Course: Day 2 - Lesson 5
Multi-line docstrings def complex(real=0.0, imag=0.0): """Form a complex number. Keyword arguments: real -- the real part (default 0.0) imag -- the imaginary part (default 0.0) """ ... Python Mini-Course: Day 2 - Lesson 5
docstring example: def perimeter(length, n_sides=4): """Print the perimeter of a regular polygon.""" p = length*n_sides print p perimeter.__doc__ Python Mini-Course: Day 2 - Lesson 5
Viewing docstrings import math, os print math.exp.__doc__ print os.getcwd__doc__ import numpy print numpy.histogram.__doc__ Python Mini-Course: Day 2 - Lesson 5