Online Book Reader

Home Category

Learning Python - Mark Lutz [4]

By Root 1369 0
a Shelve

Future Directions

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

28. Class Coding Details

The class Statement

General Form

Example

Methods

Method Example

Calling Superclass Constructors

Other Method Call Possibilities

Inheritance

Attribute Tree Construction

Specializing Inherited Methods

Class Interface Techniques

Abstract Superclasses

Python 2.6 and 3.0 Abstract Superclasses

Namespaces: The Whole Story

Simple Names: Global Unless Assigned

Attribute Names: Object Namespaces

The “Zen” of Python Namespaces: Assignments Classify Names

Namespace Dictionaries

Namespace Links

Documentation Strings Revisited

Classes Versus Modules

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

29. Operator Overloading

The Basics

Constructors and Expressions: __init__ and __sub__

Common Operator Overloading Methods

Indexing and Slicing: __getitem__ and __setitem__

Intercepting Slices

Index Iteration: __getitem__

Iterator Objects: __iter__ and __next__

User-Defined Iterators

Multiple Iterators on One Object

Membership: __contains__, __iter__, and __getitem__

Attribute Reference: __getattr__ and __setattr__

Other Attribute Management Tools

Emulating Privacy for Instance Attributes: Part 1

String Representation: __repr__ and __str__

Right-Side and In-Place Addition: __radd__ and __iadd__

In-Place Addition

Call Expressions: __call__

Function Interfaces and Callback-Based Code

Comparisons: __lt__, __gt__, and Others

The 2.6 __cmp__ Method (Removed in 3.0)

Boolean Tests: __bool__ and __len__

Object Destruction: __del__

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

30. Designing with Classes

Python and OOP

Overloading by Call Signatures (or Not)

OOP and Inheritance: “Is-a” Relationships

OOP and Composition: “Has-a” Relationships

Stream Processors Revisited

OOP and Delegation: “Wrapper” Objects

Pseudoprivate Class Attributes

Name Mangling Overview

Why Use Pseudoprivate Attributes?

Methods Are Objects: Bound or Unbound

Unbound Methods are Functions in 3.0

Bound Methods and Other Callable Objects

Multiple Inheritance: “Mix-in” Classes

Coding Mix-in Display Classes

Classes Are Objects: Generic Object Factories

Why Factories?

Other Design-Related Topics

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

31. Advanced Class Topics

Extending Built-in Types

Extending Types by Embedding

Extending Types by Subclassing

The “New-Style” Class Model

New-Style Class Changes

Type Model Changes

Diamond Inheritance Change

New-Style Class Extensions

Instance Slots

Class Properties

__getattribute__ and Descriptors

Metaclasses

Static and Class Methods

Why the Special Methods?

Static Methods in 2.6 and 3.0

Static Method Alternatives

Using Static and Class Methods

Counting Instances with Static Methods

Counting Instances with Class Methods

Decorators and Metaclasses: Part 1

Function Decorator Basics

A First Function Decorator Example

Class Decorators and Metaclasses

For More Details

Class Gotchas

Changing Class Attributes Can Have Side Effects

Changing Mutable Class Attributes Can Have Side Effects, Too

Multiple Inheritance: Order Matters

Methods, Classes, and Nested Scopes

Delegation-Based Classes in 3.0: __getattr__ and built-ins

“Overwrapping-itis”

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

Test Your Knowledge: Part VI Exercises

VII. Exceptions and Tools

32. Exception Basics

Why Use Exceptions?

Exception Roles

Exceptions: The Short Story

Default Exception Handler

Catching Exceptions

Raising Exceptions

User-Defined Exceptions

Termination Actions

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

33. Exception Coding Details

The try/except/else Statement

try Statement Clauses

The try else Clause

Example: Default Behavior

Example: Catching Built-in Exceptions

The try/finally Statement

Example: Coding Termination Actions with try/finally

Return Main Page Previous Page Next Page

®Online Book Reader