15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Each subclass must define its own constructor if desired, otherwise the base class constructor will be<br />

called. However, if a subclass overrides a base class constructor, the base class constructor will not be<br />

called automaticallysuch a request must be made explicitly as we have above. For our subclass, we<br />

make an initial call to the base class constructor before performing any "local" tasks, hence the call to<br />

AddrBookEntry. __init__() to set the name and phone number. Our subclass sets two additional instance<br />

attributes, the employee ID and e-mail address, which are set by the remaining lines of our constructor.<br />

Note how we have to explicitly pass the self instance object to the base class constructor because we<br />

are not invoking that method on an instance. We are invoking that method on an instance of a subclass.<br />

Because we are not invoking it via an instance, this unbound method call requires us to pass an<br />

acceptable instance (self) to the method.<br />

We close this section with examples of how to create an instance of the subclass, accessing its attributes<br />

and invoking its methods, including those inherited from the parent class.<br />

Using a Subclass<br />

>>> john = EmplAddrBookEntry('John Doe', '408-555-1212',<br />

42, 'john@spam.doe')<br />

Created instance for: John Doe<br />

>>> john<br />

<br />

>>> john.name<br />

'John Doe'<br />

>>> john.phone<br />

'408-555-1212'<br />

>>> john.email<br />

'john@spam.doe'<br />

>>> john.updatePhone('415-555-1212')<br />

Updated phone# for: John Doe<br />

>>> john.phone<br />

'415-555-1212'<br />

>>> john.updateEmail('john@doe.spam')<br />

Updated e-mail address for: John Doe<br />

>>> john.email<br />

'john@doe.spam'<br />

<strong>Core</strong> Style: Naming classes, attributes, and methods<br />

Class names traditionally begin with a capital letter. This is the<br />

standard convention that will help you identify classes, especially<br />

during instantiation (which would look like a function call otherwise).<br />

In particular, data attributes should sound like data value names, and<br />

method names should indicate action toward a specific object or value.<br />

Another way to phrase this is: Use nouns for data value names and<br />

predicates (verbs plus direct objects) for methods. The data items are<br />

the objects acted upon, and the methods should indicate what action<br />

the programmer wants to perform on the object.<br />

In the classes we defined above, we attempted to follow this guideline,<br />

with data values such as "name," "phone," and "email," and actions<br />

such as "updatePhone" and "updateEmail." This is known as<br />

"mixedCase" or "camelCase." The <strong>Python</strong> Style Guide favors using<br />

underscores over camelCase, i.e,. "update_phone," "update_email."<br />

Classes should also be well named; some of those good names include

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

Saved successfully!

Ooh no, something went wrong!