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.

In <strong>Python</strong>, inheritance happens when an object is qualified, and it involves searchingan attribute definition tree (one or more namespaces). Every time you use an expressionof the form object.attr (where object is an instance or class object), <strong>Python</strong>searches the namespace tree from top to bottom, beginning with object, looking forthe first attr it can find. This includes references to self attributes in your methods.Because lower definitions in the tree override higher ones, inheritance forms the basisof specialization.Attribute Tree ConstructionFigure 24-1 summarizes the way namespace trees are constructed and populatedwith names. Generally:• Instance attributes are generated by assignments to self attributes in methods.• Class attributes are created by statements (assignments) in class statements.• Superclass links are made by listing classes in parentheses in a class statementheader.ObjectsProgramSuperclassSuperclassclass S1:class S2:ClassInstanceclass X(S1, S2):def attr(self,...):self.attr = Vobject = X()object.attr?Figure 24-1. Program code creates a tree of objects in memory to be searched by attributeinheritance. Calling a class creates a new instance that remembers its class, running a classstatement creates a new class, and superclasses are listed in parentheses in the class statementheader. Each attribute reference triggers a new bottom-up tree search—even self attributes within aclass’ methods.Inheritance | 487

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

Saved successfully!

Ooh no, something went wrong!