HomePython Page 3 - Object-Oriented Programming With Python (part 2)
Alarm Bells - Python
With the basics out of the way, this concluding article discussesmore advanced aspects of Python's OO implementation, including inheritance,destructors and overrides.
So that's the theory - now let's see it in action. The first order of business is to create a new AlarmClock() class, derived from the base class Clock(). You may remember this from the first part of this article - if not, here's a reminder:
# a simple clock class
# each Clock object is initialized with offsets (hours and minutes)
# indicating the difference between GMT and local time
class Clock:
# constructor
def __init__(self, offsetSign, offsetH, offsetM, city):
# set variables to store timezone offset
# from GMT, in hours and minutes, and city name
self.offsetSign = offsetSign
self.offsetH = offsetH
self.offsetM = offsetM
self.city = city
# print message
print "Clock created"
# method to display current time, given offsets
def display(self):
# use the gmtime() function, used to convert local time to GMT
# import required methods from the time module
# returns an array
from time import time, gmtime
self.GMTTime = gmtime(time())
self.seconds = self.GMTTime[5]
self.minutes = self.GMTTime[4]
self.hours = self.GMTTime[3]
# calculate time
if(self.offsetSign == '+'):
# city time is ahead of GMT
self.minutes = self.minutes + self.offsetM
if (self.minutes > 60):
self.minutes = self.minutes - 60
self.hours = self.hours + 1
self.hours = self.hours + self.offsetH
if (self.hours >= 24):
self.hours = self.hours - 24
else:
# city time is behind GMT
self.seconds = 60 - self.seconds
self.minutes = self.minutes - self.offsetM
if (self.minutes < 0):
self.minutes = self.minutes + 60
self.hours = self.hours - 1
self.hours = self.hours - self.offsetH
if (self.hours < 0):
self.hours = 24 + self.hours
# make it look pretty and display it
self.localTime = str(self.hours) + ":" + str(self.minutes) + ":" +
str(self.seconds)
print "Local time in " + self.city + " is " + self.localTime
# that's all, folks!
And here's the derived class:
# a derived clock class
# each AlarmClock object is initialized with offsets (hours and minutes)
# indicating the difference between GMT and local time
class AlarmClock(Clock):
pass
# that's all, folks!
Let's just verify that the new class has inherited all the
methods and properties of the base class correctly.
>>> london = AlarmClock("+", 0, 00, "London")
Clock created
>>> london.display()
Local time in London is 8:52:21
>>>
Great! Next, let's add a new method to our derived class.
class AlarmClock(Clock):
# resets clock to display GMT
def reset_to_gmt(self):
self.offsetSign = "+"
self.offsetH = 0
self.offsetM = 0
self.city = "London"
print "Clock reset to GMT!"
And now, when I use it, here's what I'll see:
>>> bombay = AlarmClock("+", 5, 30, "Bombay")
Clock created
>>> bombay.display()
Local time in Bombay is 16:45:32
>>> bombay.reset_to_gmt()
Clock reset to GMT!
>>> bombay.display()
Local time in London is 11:15:39
>>>
So we have an AlarmClock() class which inherits methods from
a base Clock() class while simultaneously adding its own specialized methods. Ain't that just dandy?