Class Time with customized attribute access. : Class Definition : Class PYTHON examples


PYTHON examples » Class » Class Definition »

 

Class Time with customized attribute access.


 

class Time:
   def __init__self, hour = 0, minute = 0, second = ):
      self.hour = hour
      self.minute = minute
      self.second = second
      
   def __setattr__self, name, value ):
      if name == "hour":
         if <= value < 24:
            self.__dict__"_hour" = value
         else:
            raise ValueError, "Invalid hour value: %d" % value

      elif name == "minute" or name == "second":
         if <= 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 )
 

   
  



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON examples

 Navioo Class
» Class Definition