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. Name two ways to extend a built-in object type.2. What are function decorators used for?3. How do you code a new-style class?4. How are new-style and classic classes different?5. How are normal and static methods different?6. How long should you wait before lobbing a “Holy Hand Grenade”?Quiz Answers1. You can embed a built-in object in a wrapper class, or subclass the built-in typedirectly. The latter of these tends to be simpler, as most original behavior isautomatically inherited.2. Function decorators are generally used to add to an existing function a layer oflogic that is run each time the function is called. They can be used to log orcount calls to a function, check its argument types, and so on. They are also usedto “declare” static methods—simple functions in a class that are not passed aninstance.3. New-style classes are coded by inheriting from the object built-in class (or anyother built-in type). In <strong>Python</strong> 3.0, this will likely not be required; all classes willbe new-style classes by default.4. New-style classes search the diamond pattern of multiple inheritance trees differently—theyessentially search breadth-first (across), instead of depth-first (up).New-style classes also support a set of advanced extra tools, including propertiesand a _ _slots_ _ instance attributes list.5. Normal (instance) methods receive a self argument (the implied instance), butstatic methods do not. Static methods are simple functions nested in a classobject. To make a method static, it must be run through a special built-in function,or decorated with decorator syntax.6. Three seconds. (Or, more accurately: “And the Lord spake, saying, ‘First shaltthou take out the Holy Pin. Then, shalt thou count to three, no more, no less.Three shalt be the number thou shalt count, and the number of the countingshall be three. Four shalt thou not count, nor either count thou two, exceptingthat thou then proceed to three. Five is right out. Once the number three, beingthe third number, be reached, then lobbest thou thy Holy Hand Grenade ofAntioch towards thy foe, who, being naughty in my sight, shall snuff it.’”) ** This quote is from Monty <strong>Python</strong> and the Holy Grail.Chapter Quiz | 565

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

Saved successfully!

Ooh no, something went wrong!