12.07.2015 Views

Is Python a

Is Python a

Is Python a

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

Create successful ePaper yourself

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

B RAINB UILDERChapter Quiz1. What is an abstract superclass?2. What two operator overloading methods can you use to support iteration inyour classes?3. What happens when a simple assignment statement appears at the top level of aclass statement?4. Why might a class need to manually call the _ _init_ _ method in a superclass?5. How can you augment, instead of completely replacing, an inherited method?6. In this chapter’s final example, what methods are run when the sue Employeeinstance is printed?7. What...was the capital of Assyria?Quiz Answers1. An abstract superclass is a class that calls a method, but does not inherit ordefine it—it expects the method to be filled in by a subclass. This is often usedas a way to generalize classes when behavior cannot be predicted until a morespecific subclass is coded. OOP frameworks also use this as a way to dispatch toclient-defined, customizable operations.2. Classes can support iteration by defining (or inheriting) _ _getitem_ _ or _ _iter_ _.In all iteration contexts, <strong>Python</strong> tries to use _ _iter_ _ (which returns an object thatsupports the iteration protocol with a next method) first: if no _ _iter_ _ is foundby inheritance search, <strong>Python</strong> falls back on the _ _getitem_ _ indexing method(which is called repeatedly, with successively higher indexes).3. When a simple assignment statement (X =Y) appears at the top level of a classstatement, it attaches a data attribute to the class (Class,X). Like all classattributes, this will be shared by all instances; data attributes are not callablemethod functions, though.4. A class must manually call the _ _init_ _ method in a superclass if it defines an_ _init_ _ constructor of its own, but must still kick off the superclass’construction code. <strong>Python</strong> itself automatically runs just one constructor—thelowest one in the tree. Superclass constructors are called through the classname, passing in the self instance manually: Superclass._ _init_ _(self, ...).5. To augment instead of completely replacing an inherited method, redefine it in asubclass, but call back to the superclass’ version of the method manually fromthe new version of the method in the subclass. That is, pass the self instance tothe superclass’ version of the method manually: Superclass.method(self, ...).516 | Chapter 24: Class Coding Details

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

Saved successfully!

Ooh no, something went wrong!