27.07.2013 Views

Deitel - Python, How To Program.pdf

Deitel - Python, How To Program.pdf

Deitel - Python, How To Program.pdf

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

pythonhtp1_07.fm Page 231 Saturday, December 8, 2001 2:29 PM<br />

Chapter 7 Object-Based <strong>Program</strong>ming 231<br />

One of the fundamental principles of good software engineering is that a client should<br />

not need to know how a class is implemented to use that class. <strong>Python</strong>’s use of modules<br />

facilitates this data abstraction—the program in Fig. 7.2 simply imports the Time definition<br />

and uses class Time without knowing how the class is implemented.<br />

Software Engineering Observation 7.3<br />

Clients of a class do nost need access to the class’s source code to use the class. 7.3<br />

<strong>To</strong> create an object of class Time, simply “call” the class name as if it were a function<br />

(line 6). This call invokes the constructor for class Time. Even though the class definition<br />

stipulates that the constructor (__init__) takes one argument, line 6 does not pass any<br />

arguments to the constructor. <strong>Python</strong> inserts the first (object reference) argument into every<br />

method call, including a class’s constructor call. The constructor initializes the object’s<br />

attributes. Once the constructor exits, <strong>Python</strong> assigns the newly created object to time1.<br />

Client code must access an object’s attributes through a reference to that object. Lines<br />

10–12 demonstrate how a program can access an object’s attributes through the dot (.)<br />

access operator. The name of the object appears to the left of the dot, and the attribute<br />

appears to the right of the dot. The output demonstrates the initial values that the constructor<br />

assigned to attributes hour, minute and second.<br />

Client code can access an object’s methods in a similar manner. Line 16 calls time1’s<br />

printMilitary method. Notice again that the method call passes no arguments, even<br />

though the method definition specifies one parameter called self. <strong>Python</strong> passes a reference<br />

to time1 in the printMilitary call, so the method may access the object’s<br />

attributes.<br />

Line 23 modifies the value assigned to attribute time1.hour. The output from lines<br />

24–25 shows a problem that often arises when a client indiscriminately accesses an object’s<br />

data. The meaning of attribute hour is unclear, because that data member now has a value<br />

of 25. We say that the data member is in an inconsistent state (it contains an invalid value).<br />

Some other programming languages provide ways to prevent a client from accessing an<br />

object’s data. <strong>Python</strong>, on the other hand, does not provide such strict programming constructs.<br />

Later in this chapter, we discuss the various ways <strong>Python</strong> programmers ensure that<br />

an object’s data remains in a consistent state.<br />

Common <strong>Program</strong>ming Error 7.5<br />

Directly accessing an object’s attributes may cause the data to enter an inconsistent state. 7.4<br />

7.3 Special Attributes<br />

Classes and objects of classes both have special attributes that can be manipulated. These<br />

attributes, which <strong>Python</strong> creates when a class is defined or when an object of a class is created,<br />

provide information about the class or object of a class to which they belong.<br />

Figure 7.3 lists the special attributes that all classes contain. The interactive session in<br />

Fig. 7.4 prints the value of each of these attributes for class Time.<br />

Additionally, all objects of classes have attributes in common. Figure 7.5 lists these<br />

attributes, and the interactive session in Fig. 7.6 prints the attributes’ values for an object<br />

of class Time. Notice that objects can access the __doc__ and __module__ attributes<br />

that belong to the object’s class.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!