Classes and Functions

This discussion will continue our previous discussion by focusing on functions that manipulate objects.

This way of working with objects, i.e. having functions to process an object in some way, is called procedural programming and is the old school of coding.

Object Oriented Programming on the other hand allows for a close knit relationship between the functions and the objects they manipulate.

This is accomplished by the use of encapsulation, i.e. grouping functions along with data as attributes inside an object.

The use of such functions which are then called methods will be explored in our next discussion.

Time

Let's look at another class that can store time. The Time class looks like this:

To create a Time instance we can write:

Our first function then will be one to print a Time object in the format: hh:mm:ss, with a leading 0 if need be for each time component.

This format is possible by using :02d, which states that a 0 should be printed as the leading character in a 2 column field, if the component is only one digit.

Adding times

To add two times together can be a bit tricky if you fall back to elemententary school arithmetic, such as in:

10:50:30 +
01:40:00
--------
11:90:30
12:30:30

We need to contend with the resulting carries that seem to complicated things a bit.

A better approach is to add the two times in seconds and then convert the total number of seconds back to the three components as in:

10:50:30 = 10 * 3600 + 50 * 60 + 30 or 39,030 seconds
+
01:40:00 = 1 * 3600 + 40 * 60 + 0 or 6,000 seconds
--------
12:30:30 = 12 * 3600 + 30 * 60 + 30 or 45,030 seconds

Note how: 39,030 + 6,000 = 45,030

Let's write some functions to carry out these conversions:

Notice the use of the divmod() function that returns two results at once; the quotient and remainder.

Now, let's add another function that adds seconds to an existing time and returns a new time object.

By assigning the new Time instance to our original time reference, we in effect update time.

Our last example is one that will test if one time is after another. Although this can be accomplished using if logic, there is a better way that avoids an if all together.

Here is my version of it.

Practice problems

Create a separate Python source file (.py) in VSC to complete each exercise.


p1: multiply.py

  1. Write a function called multiplyTime() that takes a Time object and a number and returns a new Time object that contains the product of the original time and the number.

  2. Then use multiplyTime() to write a function that takes a Time object that represents the finishing time in a race, and a distance that represents the distance in miles, and returns a Time object that represents the average pace (time per mile)


p2: date.py

The datetime module provides time objects that are similar to the Time objects in our discussions, but they provide a rich set of methods and operators. Read the documentation at [https://docs.python.org/3/library/datetime.html?highlight=datetime].

Write a function called daysUntilBirthday() that takes a date object as a user's birthday and returns the number of days until the user's next birthday.

[Classes And Objects] [TOC] [Classes And Methods]