12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

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

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

ChapterCHAPTER2525Designing with Classes 25So far in this part of the book, we’ve concentrated on using <strong>Python</strong>’s OOP tool, theclass. But OOP is also about design issues—i.e., how to use classes to model usefulobjects. This chapter will touch on a few core OOP ideas, and present some additionalexamples that are more realistic than those shown so far. Many of the designterms mentioned here (delegation, composition, factories, and more) require moreexplanation than I can provide in this book; if this material sparks your curiosity, <strong>Is</strong>uggest exploring a text on OOP design, or design patterns as a next step.<strong>Python</strong> and OOP<strong>Python</strong>’s implementation of OOP can be summarized by three ideas:InheritanceInheritance is based on attribute lookup in <strong>Python</strong> (in X.name expressions).PolymorphismIn X.method, the meaning of method depends on the type (class) of X.EncapsulationMethods and operators implement behavior; data hiding is a convention bydefault.By now, you should have a good feel for what inheritance is all about in <strong>Python</strong>.We’ve also talked about <strong>Python</strong>’s polymorphism a few times already; it flows from<strong>Python</strong>’s lack of type declarations. Because attributes are always resolved at runtime,objects that implement the same interfaces are interchangeable; clients don’t need toknow what sorts of objects are implementing the methods they call.Encapsulation means packaging in <strong>Python</strong>—that is, hiding implementation detailsbehind an object’s interface. It does not mean enforced privacy, as you’ll see inChapter 26. Encapsulation allows the implementation of an object’s interface to bechanged without impacting the users of that object.518

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

Saved successfully!

Ooh no, something went wrong!