Previously, you have used built-in types in Python like dict, str, and list. Each of these types comes with its own methods, such as keys, lower,Β and append.
Every object has a typethatΒ indicates which class the object belongs to. The purpose of the class definition is to specify the instance variablesΒ (aka data, attributes) and methods (aka functions) that each object belonging to the class will have.
For example, in real life, every carhas attributes such as make, model, and current odometer. And every reasonable car has available methods such as start engine, shift transmission, and honk horn.
Based on your previous answers, how does the number of arguments for each method calldiffer from the number of parameters specified in the methodβs definition?
class Timer:
""" A class that represents a timer. """
def __init__(self):
self.hours = 0
self.minutes = 0
self.seconds = 0
def print_with_labels(self):
print('Hours:', self.hours, end=' ')
print('Minutes:', self.minutes, end=' ')
print('Seconds:', self.seconds)
def __str__(self):
return f'{self.hours}:{self.minutes}:{self.seconds}'
Recall: The __str__method returns the string representation of the object. ThisΒ methodΒ is activated (automatically!) whenever print() or str() is invoked on an object. (Like all the other methods whose names are surrounded by double underscores (__), we never call __str__ directly.Β It is there, waiting to "answer the call" when we ask to print an object or use str()to convert an object to a string.)
class Timer:
""" A class that represents a timer. """
def __init__(self):
self.hours = 0
self.minutes = 0
self.seconds = 0
def print_with_labels(self):
print('Hours:', self.hours, end=' ')
print('Minutes:', self.minutes, end=' ')
print('Seconds:', self.seconds)
def __str__(self):
return f'{self.hours}:{self.minutes}:{self.seconds}'
Which of the following is the correct definition for a new Timer class method called set that will take three integer values representing hours, minutes, and seconds and set the objectβs instance variables (attributes) appropriately?
Implement a method for the Timer class, convert_to_seconds. For a given Timer, the method returns the equivalent number of seconds. For example, if Timer t1 is currently set to 0:1:15 (0 hours, 1 minute, 15 seconds), then the call t1.convert_to_seconds() will return 75.
Hint: Remember that inside the body of the convert_to_seconds method definition, we use the parameter selfto refer to the Timerobject which activated the method.
class Timer:
""" A class that represents a timer. """
def __init__(self, h, m, s):
self.hours = h
self.minutes = m
self.seconds = s
def print_with_labels(self):
print('Hours:', self.hours, end=' ')
print('Minutes:', self.minutes, end=' ')
print('Seconds:', self.seconds)
def __str__(self):
return f'{self.hours}:{self.minutes}:{self.seconds}'
Write a function, add_timers, that takes two Timer objects, t1 and t2, and returns a new Timer object that is the sum of t1 and t2. Neither of the original Timer objects should be changed.
The minutes and seconds attributes for the new Timer object must be valid (i.e., in the range 0 through 59). This means you may need to do some conversion from seconds to minutes and from minutes to hours. You may assume that the attributes for the provided Timer objects t1 and t2 are valid.
Python has "magic" methods to make the comparison operators work with objects. The six comparison operators (<, <=, >, >=, == and !=) are enabled simply by implementing the following special methods: __lt__, __le__, __gt__, __ge__, __eq__ and __ne__
For example, here is the code to implement __lt__ (i.e., <, theΒ "less than" operator) for the Timerclass. Notice we start by converting both Timers to an integer number of seconds, then simply compare those numbers of seconds. Comparing two Timers reduces to the problem of comparing two integers.