Lesson 27 - Classes and functions
Download
Report
Transcript Lesson 27 - Classes and functions
Lesson 27
Classes and Functions
Python Mini-Course
University of Oklahoma
Department of Psychology
1
Python Mini-Course: Lesson 27
6/17/09
Lesson objectives
1. Create functions that operate on
objects
2. Differentiate between pure functions
and modifiers
3. Use argument checking in functions
2
Python Mini-Course: Lesson 27
6/17/09
The Time class
Throughout this lesson, we will be
using the custom Time class
See time.py
In IDLE, run the time.py script to
define the Time class and functions
We can now use these at the
command line
3
Python Mini-Course: Lesson 27
6/17/09
Creating time objects
time1 = Time(1,0,0)
print_time(time1)
time2 = Time(0,30,5)
print_time(time2)
4
Python Mini-Course: Lesson 27
6/17/09
Passing by reference
A variable name is a pointer to the memory
location where data are stored
When you pass an argument to a function,
you are passing in the pointer
If the function changes the value of an
argument, it changes the data in the
memory location
This is then seen outside the function
5
Python Mini-Course: Lesson 27
6/17/09
Modifier functions
Modifiers can (and usually do)
change the value of arguments
that are passed into the function
See the increment_time() function
6
Python Mini-Course: Lesson 27
6/17/09
Modifier functions: Example
increment_time(time1, time2)
print_time(time1)
print_time(time2)
7
Python Mini-Course: Lesson 27
6/17/09
Pure functions
Pure functions do not change the
value of any arguments
See the add_time() function
8
Python Mini-Course: Lesson 27
6/17/09
Pure functions: Example
time3 = add_time(time1, time2)
print_time(time1)
print_time(time2)
print_time(time3)
9
Python Mini-Course: Lesson 27
6/17/09
Argument checking
Try this:
time4 = Time(0,90,0)
increment_time(time4, time1)
increment_time(time4, 30)
10
Python Mini-Course: Lesson 27
6/17/09
Argument checking
Two problems:
1. The Time class allows invalid times
(>60 minutes or seconds)
2. The increment_time() function is
designed to accept two time
object, but the wrong data types
were passed to it
11
Python Mini-Course: Lesson 27
6/17/09
Argument checking
Solutions:
1. Check the formatting of the time
2. Check the type for the input
argument
12
Python Mini-Course: Lesson 27
6/17/09
The valid_time() function
def valid_time(t):
validity = True
# All values must be at least zero
if t.hours < 0 or t.minutes < 0 or t.seconds < 0:
validity = False
# Minutes and seconds must be base 60
if t.minutes >= 60 or t.seconds >= 60:
validity = False
return validity
13
Python Mini-Course: Lesson 27
6/17/09
Adding argument checking
def increment_time(t1, t2):
"""
Given two Time objects t1 and t2, add t2 to t1
"""
# Check the input arguments
if type(t1) != Time or type(t2) != Time:
raise AttributeError, \
'invalid argument passed to increment_time'
if not valid_time(t1) or not valid_time(t2):
raise ValueError, 'invalid Time object in increment_time'
# Add the times
t1.hour
+= t2.hour
t1.minute += t2.minute
t1.second += t2.second
14
Python Mini-Course: Lesson 27
6/17/09