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.

New-style classes are almost completely backward compatible with classic classes insyntax and behavior; they mostly just add a few advanced new features. However,because they modify one special case of inheritance, they had to be introduced as a distincttool so as to avoid impacting any existing code that depends on the prior behavior.New-style classes are coded with all the normal class syntax we have studied. Thechief coding difference is that you subclass from a built-in type (e.g., list) toproduce a new-style class. A new built-in name, object, is provided to serve as asuperclass for new-style classes if no other built-in type is appropriate to use:class newstyle(object):...normal code...More generally, any class derived from object, or any other built-in type, is automaticallytreated as a new-style class. (By derived, I mean that this includes subclasses ofobject, subclasses of subclasses of object, and so on—as long as a built-in is somewherein the superclass tree, the new class will be treated as a new-style class.)Classes not derived from built-ins are considered classic.Per <strong>Python</strong> creator Guido van Rossum, in <strong>Python</strong> 3.0, all classes willautomatically be new-style, so the requirement of being derived from abuilt-in superclass will no longer exist. Because even standaloneclasses will be considered new-style, and because new-style classes arealmost completely backward compatible with classic classes, for mostprogrammers, the change will be transparent.In the past, there was some concern that top-level classes might needto derive from object in <strong>Python</strong> 3.0, but Guido has recently stated thatthis won’t be required. To most programmers, all classes in 3.0 willwork as described in this book, but with the additional new-style featuresavailable. I can’t predict the future completely, though, so besure to check the 3.0 release notes for more on this front.Diamond Inheritance ChangePerhaps the most visible change in new-style classes is their slightly different treatmentof inheritance for the so-called diamond pattern of multiple inheritance trees,where more than one superclass leads to the same higher superclass further above.The diamond pattern is an advanced design concept that we have not even discussedfor normal classes.In short, with classic classes, the inheritance search procedure is strictly depth first,and then left to right—<strong>Python</strong> climbs all the way to the top, hugging the left side ofthe tree, before it backs up, and begins to look further to the right. In new-styleclasses, the search is more breadth-first in such cases—<strong>Python</strong> first looks in anysuperclasses to the right of the first one searched before ascending all the way to thecommon superclass at the top. Because of this change, lower superclasses can overloadattributes of higher superclasses, regardless of the sort of multiple inheritancetrees they are mixed into.546 | Chapter 26: Advanced Class Topics

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

Saved successfully!

Ooh no, something went wrong!