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.

Chapter 23CHAPTER 23Class Coding Basics23Now that we’ve talked about OOP in the abstract, it’s time to see how this translatesto actual code. This chapter and the next will fill in the syntax details behind theclass model in <strong>Python</strong>.If you’ve never been exposed to OOP in the past, classes can seem somewhat complicatedif taken in a single dose. To make class coding easier to absorb, we’ll begin ourdetailed exploration of OOP by taking a first look at some basic classes in action inthis chapter. We’ll expand on the details introduced here in later chapters of this partof the book, but in their basic form, <strong>Python</strong> classes are easy to understand.Classes have three primary distinctions. At a base level, they are mostly justnamespaces, much like the modules we studied in Part V. But, unlike modules,classes also have support for generating multiple objects, for namespace inheritance,and for operator overloading. Let’s begin our class statement tour by exploring eachof these three distinctions in turn.Classes Generate Multiple Instance ObjectsTo understand how the multiple objects idea works, you have to first understandthat there are two kinds of objects in <strong>Python</strong>’s OOP model: class objects, andinstance objects. Class objects provide default behavior, and serve as factories forinstance objects. Instance objects are the real objects your programs process—each isa namespace in its own right, but inherits (i.e., has automatic access to) names in theclass from which it was created. Class objects come from statements, and instancesfrom calls; each time you call a class, you get a new instance of that class.This object-generation concept is very different from any of the other program constructswe’ve seen so far in this book. In effect, classes are factories for generatingmultiple instances. By contrast, only one copy of each module is ever imported into asingle program (in fact, one reason that we have to call reload is to update the singlemodule object so that changes are reflected once they’ve been made).465

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

Saved successfully!

Ooh no, something went wrong!