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.

superclasses that have empty _ _bases_ _ attributes. Most of this file is self-test code;when run standalone, it builds an empty class tree, makes two instances from it, andprints their class tree structures:% python classtree.pyTree of ... B...... ATree of ... F...... D......... B............ A......... C............ A...... EHere, indentation marked by periods is used to denote class tree height. Of course, wecould improve on this output format, and perhaps even sketch it in a GUI display.We can import these functions anywhere we want a quick class tree display:>>> class Emp: pass...>>> class Person(Emp): pass...>>> bob = Person( )>>> import classtree>>> classtree.instancetree(bob)Tree of ... Person...... EmpWhether you will ever code or use such tools, this example demonstrates one of themany ways that you can make use of special attributes that expose interpreter internals.You’ll see another when we code a general-purpose attribute-listing class in the“Multiple Inheritance” section of Chapter 25.A More Realistic ExampleMost of the examples we’ve looked at so far have been artificial and self-contained tohelp you focus on the basics. However, we’ll close out this chapter with a largerexample that pulls together much of what we’ve studied here. I’m including thismostly as a self-study exercise—try to trace through this example’s code to see howmethod calls are resolved.In short, the following module, person.py, defines three classes:• GenericDisplay is a mix-in class that provides a generic _ _str_ _ method; for anyclass that inherits from it, this method returns a string giving the name of theclass from which the instance was created, as well as “name=value” pairs for512 | Chapter 24: Class Coding Details

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

Saved successfully!

Ooh no, something went wrong!