Python
  Home arrow Python arrow Page 3 - Object-Oriented Programming With Pytho...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Object-Oriented Programming With Python (part 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 9
    2001-11-26

    Table of Contents:
  • Object-Oriented Programming With Python (part 2)
  • The Family Tree
  • Alarm Bells
  • Under The Microscope
  • Chaos And Destruction

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Object-Oriented Programming With Python (part 2) - Alarm Bells


    (Page 3 of 5 )

    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?

    More Python Articles
    More By icarus, (c) Melonfire


     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway