day 2 – lesson 5 function interfaces

20
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 5 Function Interfaces 4/18/09 Python Mini-Course: Day 2 - Lesson 5 1

Upload: dylan-shepard

Post on 30-Dec-2015

31 views

Category:

Documents


2 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Day 2 – Lesson 5 Function Interfaces

Python Mini-CourseUniversity of Oklahoma

Department of Psychology

Day 2 – Lesson 5Function Interfaces

4/18/09Python Mini-Course: Day 2 - Lesson 51

Page 2: Day 2 – Lesson 5 Function Interfaces

Questions from Day 1

InstallationPython syntaxVariables and data typesCreating and using functionsUsing the math library

4/18/09Python Mini-Course: Day 2 - Lesson 52

Page 3: Day 2 – Lesson 5 Function Interfaces

Lesson objectives

1. State the principles of good function interface design

2. Define encapsulation and state why it is useful

3. Use parameters to make functions generalizable

4. Use docstrings to document function interfaces

4/18/09Python Mini-Course: Day 2 - Lesson 53

Page 4: Day 2 – Lesson 5 Function Interfaces

Designing function interfaces

InterfaceA summary of how the function is used

Documentation specifies:What the function doesParameters to be passed into the

functionWhat (if anything) the function

returns

4/18/09Python Mini-Course: Day 2 - Lesson 54

Page 5: Day 2 – Lesson 5 Function Interfaces

Designing function interfaces

Design principles:1. KISS2. Encapsulate3. 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.’”

4. Document!!!

4/18/09Python Mini-Course: Day 2 - Lesson 55

Page 6: Day 2 – Lesson 5 Function Interfaces

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)

4/18/09Python Mini-Course: Day 2 - Lesson 56

Page 7: Day 2 – Lesson 5 Function Interfaces

Example

Driving a carYou need to know how to use:

Gas and brake petalsSteering wheelGear shifter

You don’t need to know:The kind of engineHow the engine (or any other system)

works

4/18/09Python Mini-Course: Day 2 - Lesson 57

Page 8: Day 2 – Lesson 5 Function Interfaces

Why encapsulate?

Makes it easier to change the internal mechanisms

Protects the integrity of the componentprevents users from setting the

internal data of the component into an invalid or inconsistent state

Reduces system complexity

4/18/09Python Mini-Course: Day 2 - Lesson 58

Page 9: Day 2 – Lesson 5 Function Interfaces

Code example

def perimeter(length): p = length + length + length \ + length print p

perimeter(3)

4/18/09Python Mini-Course: Day 2 - Lesson 59

Page 10: Day 2 – Lesson 5 Function Interfaces

Code example

def perimeter(length): p = 4*length print p

perimeter(3)

4/18/09Python Mini-Course: Day 2 - Lesson 510

Page 11: Day 2 – Lesson 5 Function Interfaces

Code example

def perimeter(length, n_sides): p = length*n_sides print p

perimeter(3, 4)perimeter(3, 6)

4/18/09Python Mini-Course: Day 2 - Lesson 511

Page 12: Day 2 – Lesson 5 Function Interfaces

Designing for generalizability

Try to think of all the possible uses for this function

Avoid hard-codingUse parameters instead

4/18/09Python Mini-Course: Day 2 - Lesson 512

Page 13: Day 2 – Lesson 5 Function Interfaces

Parameters and arguments

Parameters are the values that a function receives as inputdef perimeter(length, n_sides):

You can also use parameters that specify a default valuedef perimeter(length, n_sides=4):

4/18/09Python Mini-Course: Day 2 - Lesson 513

Page 14: Day 2 – Lesson 5 Function Interfaces

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)

4/18/09Python Mini-Course: Day 2 - Lesson 514

Page 15: Day 2 – Lesson 5 Function Interfaces

Using docstrings

A string literal that occurs as the first statement in a module, function, class, or method definitionbecomes the __doc__ special attribute of that object

See PEP-257 for detailshttp://www.python.org/dev/peps/pep-0257/

4/18/09Python Mini-Course: Day 2 - Lesson 515

Page 16: Day 2 – Lesson 5 Function Interfaces

One-line docstrings

For really obvious casesdef 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

4/18/09Python Mini-Course: Day 2 - Lesson 516

Page 17: Day 2 – Lesson 5 Function Interfaces

One-line docstrings

Should be a phrase ending in a periodPrescribes the function or method's

effect as a command, not as a description"""Do this.""" or """Return that."""NOT """Returns the pathname ..."""

4/18/09Python Mini-Course: Day 2 - Lesson 517

Page 18: Day 2 – Lesson 5 Function Interfaces

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)

"""

...

4/18/09Python Mini-Course: Day 2 - Lesson 518

Page 19: Day 2 – Lesson 5 Function Interfaces

docstring example:

def perimeter(length, n_sides=4): """Print the perimeter of a regular polygon.""" p = length*n_sides print p

perimeter.__doc__

4/18/09Python Mini-Course: Day 2 - Lesson 519

Page 20: Day 2 – Lesson 5 Function Interfaces

Viewing docstrings

import math, osprint math.exp.__doc__print os.getcwd__doc__

import numpyprint numpy.histogram.__doc__

4/18/09Python Mini-Course: Day 2 - Lesson 520