1.9 Preconditions and PostconditionsWhen defining the operations of an ADT, we must include a specification of required inputs and the resulting output, if any. In addition, we must specify the preconditions and postconditions for each operation. A precondition indicates the condition or state of the ADT instance and inputs before the operation can be performed. A postcondition indicates the result or ending state of the ADT instance after the operation is performed. The precondition is assumed to be true while the postcondition is a guarantee as long as the preconditions are met. Attempting to perform an operation in which the precondition is not satisfied should be flagged as an error. Consider the use of the myList = [0, 1, 2, 3, 4, 5] i = 2 myList.pop(i) When this method is called, the precondition states the supplied index must be within the legal range. Upon successful completion of the operation, the postcondition guarantees the item has been removed from the list. If an invalid index, one that is out of the legal range, is passed to the Now, consider the following code segment myList = [0, 1, 2, 3, 4, 5] myList.pop(8) which results in an "Index out of range" exception Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: pop index out of range because we are trying to pop an element beyond the end of the list. All operations have at least one precondition, which is that the ADT instance has to have been previously initialized. In an object-oriented language, this precondition is automatically verified since an object must be created and initialized via the constructor before any operation can be used. Other than the initialization requirement, some operations may not have any other preconditions. It all depends on the type of ADT and the respective operation. Likewise, some operations may not have a postcondition, as is the case for simple access methods, which simply return a value without modifying the ADT instance itself. Throughout the text, we do not explicitly state the precondition and postcondition as such, but they are easily identified from the description of the ADT operations. For example, the definition of the When implementing abstract data types, it is important that we ensure the proper execution of the various operations by verifying any stated preconditions. The appropriate mechanism when testing preconditions for abstract data types is to test the precondition and raise an exception when the precondition fails. You then allow the user of the ADT to decide how they wish to handle the error, either catch it or allow the program to abort. Python, like many other object-oriented programming languages, raises an exception when an error occurs. An exception is an event that can be triggered and optionally handled during program execution. When an exception is raised indicating an error, the program can contain code to catch and gracefully handle the exception; otherwise, the program will abort. Python also provides the Returning to the implementation of the assert not self.isVertical(), "Line segment can not be vertical." If the assertion fails, which will happen when the line is vertical, Python will raise an Traceback (most recent call last): File "line.py", line 4, in <module> AssertionError: Line segment can not be vertical. The last line of the error message includes the string provided as the second argument to the The new implementation of the def slope(self) : assert not self.isVertical(), "Line segment can not be vertical." rise = self._yCoordA - self._yCoordB run = self._xCoordA - self._xCoordB return rise / run Throughout the text, we use the A new listing of our implementation of the Program Listing
|