Python Mini-Course - PowerPoint PPT Presentation

About This Presentation
Title:

Python Mini-Course

Description:

time1 = Time(1,0,0) print_time(time1) time2 = Time(0,30,5) ... increment_time(time4, time1) increment_time(time4, 30) 6/17/09. Python Mini-Course: Lesson 27 ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 15
Provided by: troys9
Learn more at: https://www.ou.edu
Category:
Tags: course | mini | python | time1

less

Transcript and Presenter's Notes

Title: Python Mini-Course


1
Lesson 27Classes and Functions
  • Python Mini-Course
  • University of Oklahoma
  • Department of Psychology

2
Lesson objectives
  1. Create functions that operate on objects
  2. Differentiate between pure functions and
    modifiers
  3. Use argument checking in functions

3
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

4
Creating time objects
  • time1 Time(1,0,0)
  • print_time(time1)
  • time2 Time(0,30,5)
  • print_time(time2)

5
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

6
Modifier functions
  • Modifiers can (and usually do) change the value
    of arguments that are passed into the function
  • See the increment_time() function

7
Modifier functions Example
  • increment_time(time1, time2)
  • print_time(time1)
  • print_time(time2)

8
Pure functions
  • Pure functions do not change the value of any
    arguments
  • See the add_time() function

9
Pure functions Example
  • time3 add_time(time1, time2)
  • print_time(time1)
  • print_time(time2)
  • print_time(time3)

10
Argument checking
  • Try this
  • time4 Time(0,90,0)
  • increment_time(time4, time1)
  • increment_time(time4, 30)

11
Argument checking
  • Two problems
  • The Time class allows invalid times (gt60 minutes
    or seconds)
  • The increment_time() function is designed to
    accept two time object, but the wrong data types
    were passed to it

12
Argument checking
  • Solutions
  • Check the formatting of the time
  • Check the type for the input argument

13
The valid_time() function
  • def valid_time(t)
  • validity True
  • All values must be at least zero
  • if t.hours lt 0 or t.minutes lt 0 or t.seconds
    lt 0
  • validity False
  • Minutes and seconds must be base 60
  • if t.minutes gt 60 or t.seconds gt 60
  • validity False
  • return validity

14
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
Write a Comment
User Comments (0)
About PowerShow.com