Time Definition¶__init__ Method with Default Parameter Values¶hour, minute and second parameters, each with a default argument of 0self.hour, self.minute and self.second appear to create hour, minute and second attributes for the new Time object (self)hour, minute and second properties _hour, _minute and _second # timewithproperties.py
"""Class Time with read-write properties."""
class Time:
"""Class Time with read-write properties."""
def __init__(self, hour=0, minute=0, second=0):
"""Initialize each attribute."""
self.hour = hour # 0-23
self.minute = minute # 0-59
self.second = second # 0-59
hour Read-Write Property¶hour define a publicly accessible read-write property named hour that manipulates a data attribute named _hour_) naming convention indicates that client code should not access _hour directlyTime objects, but are implemented as methods@property
def hour(self):
"""Return the hour."""
return self._hour
@hour.setter
def hour(self, hour):
"""Set the hour."""
if not (0 <= hour < 24):
raise ValueError(f'Hour ({hour}) must be 0-23')
self._hour = hour
hour Read-Write Property (cont.)¶@property decorator precedes the property’s getter method, which receives only a self parameterhour function work with attribute syntaxhour Read-Write Property (cont.)¶@property_name.setter (@hour.setter) precedes the property’s setter methodself and a parameter (hour) representing the value being assigned to the property__init__ invoked this setter to validate __init__’s hour argument before creating and initializing the object’s _hour attributeminute and second Read-Write Properties¶minute and second define read-write minute and second propertiessetter ensures that its second argument is in the range 0–59 (the valid range of values for minutes and seconds)@property
def minute(self):
"""Return the minute."""
return self._minute
@minute.setter
def minute(self, minute):
"""Set the minute."""
if not (0 <= minute < 60):
raise ValueError(f'Minute ({minute}) must be 0-59')
self._minute = minute
@property
def second(self):
"""Return the second."""
return self._second
@second.setter
def second(self, second):
"""Set the second."""
if not (0 <= second < 60):
raise ValueError(f'Second ({second}) must be 0-59')
self._second = second
set_time¶set_time changes all three attributes with a single method calldef set_time(self, hour=0, minute=0, second=0):
"""Set values of hour, minute, and second."""
self.hour = hour
self.minute = minute
self.second = second
__repr__¶repr—which happens implicitly when you evaluate a variable in an IPython session—the corresponding class’s __repr__ special method is called to get a string representation of the objectdef __repr__(self):
"""Return Time string for repr()."""
return (f'Time(hour={self.hour}, minute={self.minute}, ' +
f'second={self.second})')
__repr__ (cont.)¶__repr__ returns the “official” string representation of the object__str__¶__str__ special method is called implicitly when you strprint an object def __str__(self):
"""Print Time in 12-hour clock format."""
return (('12' if self.hour in (0, 12) else str(self.hour % 12)) +
f':{self.minute:0>2}:{self.second:0>2}' +
(' AM' if self.hour < 12 else ' PM'))
©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.
DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.