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.

Although less common, we could even generate a brand new attribute in theinstance’s namespace by assigning to its name outside the class’s method functions:>>> x.anothername = "spam" # Can set new attributes here tooThis would attach a new attribute called anothername, which may or may not be usedby any of the class’ methods to the instance object x. Classes usually create all of theinstance’s attributes by assignment to the self argument, but they don’t have to;programs can fetch, change, or create attributes on any objects to which they havereferences.Classes Are Customized by InheritanceBesides serving as factories for generating multiple instance objects, classes alsoallow us to make changes by introducing new components (called subclasses),instead of changing existing components in-place. Instance objects generated from aclass inherit the class’ attributes. <strong>Python</strong> also allows classes to inherit from otherclasses, opening the door to coding hierarchies of classes that specialize behavior byoverriding existing attributes lower in the hierarchy. Here, too, there is no parallelwith modules: their attributes live in a single, flat namespace.In <strong>Python</strong>, instances inherit from classes, and classes inherit from superclasses. Hereare the key ideas behind the machinery of attribute inheritance:• Superclasses are listed in parentheses in a class header. To inherit attributesfrom another class, just list the class in parentheses in a class statement’sheader. The class that inherits is called a subclass, and the class that is inheritedfrom is its superclass.• Classes inherit attributes from their superclasses. Just as instances inherit theattribute names defined in their classes, classes inherit all the attribute namesdefined in their superclasses; <strong>Python</strong> finds them automatically when they’reaccessed, if they don’t exist in the subclasses.• Instances inherit attributes from all accessible classes. Each instance gets namesfrom the class it’s generated from, as well as all of that class’ superclasses. Whenlooking for a name, <strong>Python</strong> checks the instance, then its class, then all superclasses.• Each object.attribute reference invokes a new, independent search. <strong>Python</strong>performs an independent search of the class tree for each attribute fetch expression.This includes references to instances and classes made outside classstatements (e.g., X.attr), as well as references to attributes of the self instanceargument in class method functions. Each self.attr expression in a methodinvokes a new search for attr in self and above.• Logic changes are made by subclassing, not by changing superclasses. By redefiningsuperclass names in subclasses lower in the hierarchy (tree), subclasses replaceand thus customize inherited behavior.Classes Are Customized by Inheritance | 469

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

Saved successfully!

Ooh no, something went wrong!