Over 10 Million Study Resources Now at Your Fingertips

Download as :
Rating : ⭐⭐⭐⭐⭐
Price : $10.99
Pages: 2

The keywordof the exception and the exceptions attribute value

pythonhtp1_07.fm Page 237 Saturday, December 8, 2001 2:29 PM

Chapter 7

Common Programming Error 7.6

When inside a method, forgetting to use the object reference (often called self) to access another method defined by the object’s class is either a fatal runtime error or a logic error. The logic error occurs when the global namespace contains a function with the same name as one of the class’s methods. In this case, forgetting to access the method name through the object reference actually calls the global function. 7.6

Chained comparison expressions can be more efficient than their non-chained equivalents,

because each term in the chained comparison expression is evaluated only once.

checks whether the value passed as a parameter is in the range 0–23, inclusive. If the hour
is valid, the method updates attribute _hour with the new value. Otherwise, the method
raises an exception, to indicate that the client has attempted to place the object’s data in an
inconsistent state. An exception is a Python object that indicates a special event (most often
an error) has occurred. For example, when a program attempts to access a nonexistent dic-
the exception can go uncaught, in which case the program prints an error message and ter-
minates immediately. Catching and handling an exception enables a program to recognize
and potentially fix errors that might otherwise cause a program to terminate. For example,
a client that uses class Time can catch an exception and detect that the program has
attempted to place data in an inconsistent state (i.e., set an invalid time). Catching and han-
dling exceptions is a broad topic that we discuss in detail in Chapter 12, Exception Han-
dling. For now, we discuss only how to raise an exception to indicate invalid data

assignments and prevent data corruption.

The statement in line 27 uses keyword raise to raise an exception. The keyword

raise is followed by the name of the exception, a comma and a value that the exception
object stores as an attribute. When Python executes a raise statement, an exception is
raised; if the exception is not caught, Python prints an error message that contains the name
change attributes _minute and _second, respectively. Each method ensures that the
values remain in the range 0–59, inclusive. If the values are invalid, the methods raise

pythonhtp1_07.fm Page 238 Saturday, December 8, 2001 2:29 PM

Chapter 7
Fig. 7.8

Raising an exception.

Lines 45–58 contain the get methods for class Time. Clients use these methods (getHour, getMinute and getSecond) to retrieve the values of an attributes _hour, _minute and _second, respectively. The remainder of the class definition does not differ from the previous definition we presented.

sistent state.


Software Engineering Observation 7.9


Every method that modifies the data of an object should ensure that the data remains in a

consistent state. 7.10


# print initial time
print"The initial military time is", time1.printMilitary()
print"\nThe initial standard time is", time1.printStandard()

Fig. 7.9
How It Works
Login account
Login Your Account
Add to cart
Add to Cart
Make payment
Document download
Download File
PageId: ELI8F260C1
Uploaded by :
Page 1 Preview
the keywordof the exception and the exceptions att
Sell Your Old Documents & Earn Wallet Balance