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.

interfacing with components of the RepairShop), or aggregation, encapsulating components that are then<br />

accessed only via defined interfaces, and again, hidden from the client programmer. Continuing our<br />

example, the client programmer may be able to make a SmogCheck request on behalf of the Customer, but<br />

has no ability to interact with the SmogZone part of the RepairShop, which is accessed only via internal<br />

controls of the RepairShop when the smogCheckCar() method is called. Both forms of composition are<br />

supported in <strong>Python</strong>.<br />

Derivation/Inheritance/Hierarchy<br />

Derivation describes the creation of subclasses, new classes that retain all desired data and behavior of<br />

the existing class type but permit modification or other customization, all without having to modify the<br />

original class definition. Inheritance describes the means by which attributes of a subclass are<br />

"bequeathed from" an ancestor class. From our earlier example, a Mechanic may have more car skill<br />

attributes than a Customer, but individually, each "is a" Person, so it is valid to invoke the talk() method,<br />

which is common to all instances of Person, for either of them. Hierarchy describes multiple<br />

"generations" of derivation which can be depicted graphically as a "family tree," with successive<br />

subclasses having relationships with ancestor classes.<br />

Generalization/Specialization<br />

Generalization describes all the traits a subclass has with its parent and ancestor classes, so subclasses<br />

are considered to have an "is-a" relationship with ancestor classes because a derived object (instance) is<br />

an "example" of an ancestor class. For example, a Mechanic "is a" Person, a Car "is a" Vehicle, etc. In the<br />

family tree diagram we alluded to above, we can draw lines from subclasses to ancestors indicating "isa"<br />

relationships. Specialization is the term that describes all the customization of a subclass, i.e., what<br />

attributes make it differ from its ancestor classes.<br />

Polymorphism<br />

The concept of polymorphism describes how objects can be manipulated and accessed using attributes<br />

and behaviors they have in common without regard to their specific class. Polymorphism indicates the<br />

presence of dynamic (aka late, runtime) binding, allowing for overriding and runtime type determination<br />

and verification.<br />

Introspection/Reflection<br />

Introspection is what gives you, the programmer, the ability to perform an activity such as "manual type<br />

checking." Also called reflection, this property describes how information about a particular object can<br />

be accessed by itself during runtime. Would it not be great to have the ability to take an object passed<br />

to you and be able to find out what it is capable of? This is a powerful feature that you will encounter<br />

frequently in this chapter. The dir() and type() built-in functions would have a very difficult time<br />

working if <strong>Python</strong> did not support some sort of introspection capability. Keep an eye out for these calls<br />

as well as for special attributes like __dict__, __name__, and __doc__. You may even be familiar with<br />

some of them already!

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

Saved successfully!

Ooh no, something went wrong!