Simple definition of class Time. : Class Definition « Class « Python Tutorial






class Time:
   def __init__( self ):
      self.hour = 0     # 0-23
      self.minute = 0   # 0-59
      self.second = 0   # 0-59

   def printMilitary( self ):
      print "%.2d:%.2d:%.2d" % ( self.hour, self.minute, self.second ),
 
   def printStandard( self ):
      standardTime = ""
      if self.hour == 0 or self.hour == 12:
         standardTime += "12:"
      else:
         standardTime += "%d:" % ( self.hour % 12 )

      standardTime += "%.2d:%.2d" % ( self.minute, self.second )

      if self.hour < 12:
         standardTime += " AM"
      else:
         standardTime += " PM"
      
      print standardTime,








11.9.Class Definition
11.9.1.Demonstrates a basic class and object
11.9.2.Creating a Class (Class Definition)
11.9.3.Classes and Types
11.9.4.Defining Class Methods with the def Statement
11.9.5.Rectangle class
11.9.6.Throwing Methods Around
11.9.7.Simple definition of class Time.
11.9.8.Definition of class Date.