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.
Let's look at another class that can store time. The Time
class looks like this:
class Time:
''' Represent the time of day.
attributes: hour, minute, second'''
To create a Time
instance we can write:
time = Time()
time.hour, time.minute, time.second = 11, 9, 30
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.
def printTime(time):
print(f'{time.hour:02d}:{time.minute:02d}:{time.second:02d}')
printTime(time)
11:09:30
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:
def time2seconds(time):
return time.hour * 3600 + time.minute * 60 + time.second
def seconds2time(sec):
time = Time()
time.hour, sec = divmod(sec, 3600)
time.minute, time.second = divmod(sec, 60)
return time
def addTime(time1, time2):
sec1 = time2seconds(time1)
sec2 = time2seconds(time2)
sec = sec1 + sec2
return seconds2time(sec)
# Movie start time.
start = Time()
start.hour, start.minute, start.second = 9, 45, 0
# Movie duration.
duration = Time()
duration.hour, duration.minute, duration.second = 1, 35, 0
# Movie end time.
end = addTime(start, duration)
printTime(end)
11:20:00
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.
def addSeconds(time, sec):
sec1 = time2seconds(time)
return seconds2time(sec1 + sec)
time = Time()
time.hour, time.minute, time.second = 0, 0, 0
time = addSeconds(time, 100)
printTime(time)
00:01:40
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.
def isAfter(lhs, rhs):
'''Returns True if lhs time is after rhs time.'''
# Use tuple comparison
return (lhs.hour, lhs.minute, lhs.second) > (rhs.hour, rhs.minute, rhs.second)
lhs, rhs = Time(), Time()
lhs.hour, lhs.minute, lhs.second = 1, 10, 40
rhs.hour, rhs.minute, rhs.second = 1, 20, 30
print(isAfter(lhs, rhs), isAfter(rhs, lhs))
False True
Create a separate Python source file (.py) in VSC to complete each exercise.
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.
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)
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.