Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python Mini-Course University of Oklahoma Department of Psychology Lesson 27 Classes and Functions 6/17/09 Python Mini-Course: Lesson 27 1.

Similar presentations


Presentation on theme: "Python Mini-Course University of Oklahoma Department of Psychology Lesson 27 Classes and Functions 6/17/09 Python Mini-Course: Lesson 27 1."— Presentation transcript:

1 Python Mini-Course University of Oklahoma Department of Psychology Lesson 27 Classes and Functions 6/17/09 Python Mini-Course: Lesson 27 1

2 Lesson objectives 1. Create functions that operate on objects 2. Differentiate between pure functions and modifiers 3. Use argument checking in functions 6/17/09 Python Mini-Course: Lesson 27 2

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 6/17/09 Python Mini-Course: Lesson 27 3

4 Creating time objects time1 = Time(1,0,0) print_time(time1) time2 = Time(0,30,5) print_time(time2) 6/17/09 Python Mini-Course: Lesson 27 4

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/17/09 Python Mini-Course: Lesson 27 5

6 Modifier functions Modifiers can (and usually do) change the value of arguments that are passed into the function See the increment_time() function 6/17/09 Python Mini-Course: Lesson 27 6

7 Modifier functions: Example increment_time(time1, time2) print_time(time1) print_time(time2) 6/17/09 Python Mini-Course: Lesson 27 7

8 Pure functions Pure functions do not change the value of any arguments See the add_time() function 6/17/09 Python Mini-Course: Lesson 27 8

9 Pure functions: Example time3 = add_time(time1, time2) print_time(time1) print_time(time2) print_time(time3) 6/17/09 Python Mini-Course: Lesson 27 9

10 Argument checking Try this: time4 = Time(0,90,0) increment_time(time4, time1) increment_time(time4, 30) 6/17/09 Python Mini-Course: Lesson 27 10

11 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 6/17/09 Python Mini-Course: Lesson 27 11

12 Argument checking Solutions: 1. Check the formatting of the time 2. Check the type for the input argument 6/17/09 Python Mini-Course: Lesson 27 12

13 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 6/17/09 Python Mini-Course: Lesson 27 13

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 6/17/09 Python Mini-Course: Lesson 27 14


Download ppt "Python Mini-Course University of Oklahoma Department of Psychology Lesson 27 Classes and Functions 6/17/09 Python Mini-Course: Lesson 27 1."

Similar presentations


Ads by Google