Having said that I should also note that even though you might not code diamond

Having said that i should also note that even though

This preview shows page 179 - 181 out of 214 pages.

Having said that, I should also note that even though you might not code diamond patterns in classes you write yourself, because the implied object superclass is above every class in 3.0, every case of multiple inheritance exhibits the diamond pattern today. That is, in new-style classes, object automatically plays the role that the class A does in the example we just considered. Hence the new-style search rule not only modifies logical semantics, but also optimizes performance by avoiding visiting the same class more than once. Just as important, the implied object superclass in the new-style model provides default methods for a variety of built-in operations, including the __str__ and __repr__ display format methods. Run a dir(object) to see which methods are provided. Without the new-style search order, in multiple inheritance cases the defaults in object would al- ways override redefinitions in user-coded classes, unless they were always made in the leftmost superclass. In other words, the new-style class model itself makes using the new-style search order more critical! New-Style Class Changes | 789 Copyright © ${Date}. ${Publisher}. All rights reserved.
Image of page 179
For a more visual example of the implied object superclass in 3.0, and other examples of diamond patterns created by it, see the ListTree class’s output in the lister.py example in the preceding chapter, as well as the classtree.py tree walker example in Chapter 28 . New-Style Class Extensions Beyond the changes described in the prior section (which, frankly, may be too academic and obscure to matter to many readers of this book), new-style classes provide a handful of more advanced class tools that have more direct and practical application. The fol- lowing sections provide an overview of each of these additional features, available for new-style class in Python 2.6 and all classes in Python 3.0. Instance Slots By assigning a sequence of string attribute names to a special __slots__ class attribute, it is possible for a new-style class to both limit the set of legal attributes that instances of the class will have and optimize memory and speed performance. This special attribute is typically set by assigning a sequence of string names to the variable __slots__ at the top level of a class statement: only those names in the __slots__ list can be assigned as instance attributes. However, like all names in Python, instance attribute names must still be assigned before they can be referenced, even if they’re listed in __slots__ . For example: >>> class limiter(object): ... __slots__ = ['age', 'name', 'job'] ... >>> x = limiter() >>> x.age # Must assign before use AttributeError: age >>> x.age = 40 >>> x.age 40 >>> x.ape = 1000 # Illegal: not in __slots__ AttributeError: 'limiter' object has no attribute 'ape' Slots are something of a break with Python’s dynamic nature, which dictates that any name may be created by assignment. However, this feature is envisioned as both a way to catch “typo” errors like this (assignments to illegal attribute names not in __slots__ are detected), as well as an optimization mechanism. Allocating a namespace
Image of page 180
Image of page 181

You've reached the end of your free preview.

Want to read all 214 pages?

  • Fall '15

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture