Class Time with customized attribute access. : Class Definition « Class « Python






Class Time with customized attribute access.

 

class Time:
   def __init__( self, hour = 0, minute = 0, second = 0 ):
      self.hour = hour
      self.minute = minute
      self.second = second
      
   def __setattr__( self, name, value ):
      if name == "hour":
         if 0 <= value < 24:
            self.__dict__[ "_hour" ] = value
         else:
            raise ValueError, "Invalid hour value: %d" % value

      elif name == "minute" or name == "second":
         if 0 <= value < 60:
            self.__dict__[ "_" + name ] = value
         else:
            raise ValueError, "Invalid %s value: %d" % ( name, value )

      else:
         self.__dict__[ name ] = value

   def __getattr__( self, name ):
      if name == "hour":
         return self._hour
      elif name == "minute":
         return self._minute
      elif name == "second":
         return self._second
      else:
         raise AttributeError, name
 
   def __str__( self ):
      return "%.2d:%.2d:%.2d" % ( self._hour, self._minute, self._second )
 

   
  








Related examples in the same category

1.Class Time with properties
2.Simple class with slots
3.Definition and test function for class Point.
4.User-Defined Classes
5.NameTag class
6.Class instance value setting
7.Define a class