Online Book Reader

Home Category

Learning Python - Mark Lutz [3]

By Root 1385 0

Enclosing Scope Loop Variables

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

Test Your Knowledge: Part IV Exercises

V. Modules

21. Modules: The Big Picture

Why Use Modules?

Python Program Architecture

How to Structure a Program

Imports and Attributes

Standard Library Modules

How Imports Work

1. Find It

2. Compile It (Maybe)

3. Run It

The Module Search Path

Configuring the Search Path

Search Path Variations

The sys.path List

Module File Selection

Advanced Module Selection Concepts

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

22. Module Coding Basics

Module Creation

Module Usage

The import Statement

The from Statement

The from * Statement

Imports Happen Only Once

import and from Are Assignments

Cross-File Name Changes

import and from Equivalence

Potential Pitfalls of the from Statement

Module Namespaces

Files Generate Namespaces

Attribute Name Qualification

Imports Versus Scopes

Namespace Nesting

Reloading Modules

reload Basics

reload Example

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

23. Module Packages

Package Import Basics

Packages and Search Path Settings

Package __init__.py Files

Package Import Example

from Versus import with Packages

Why Use Package Imports?

A Tale of Three Systems

Package Relative Imports

Changes in Python 3.0

Relative Import Basics

Why Relative Imports?

The Scope of Relative Imports

Module Lookup Rules Summary

Relative Imports in Action

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

24. Advanced Module Topics

Data Hiding in Modules

Minimizing from * Damage: _X and __all__

Enabling Future Language Features

Mixed Usage Modes: __name__ and __main__

Unit Tests with __name__

Using Command-Line Arguments with __name__

Changing the Module Search Path

The as Extension for import and from

Modules Are Objects: Metaprograms

Importing Modules by Name String

Transitive Module Reloads

Module Design Concepts

Module Gotchas

Statement Order Matters in Top-Level Code

from Copies Names but Doesn’t Link

from * Can Obscure the Meaning of Variables

reload May Not Impact from Imports

reload, from, and Interactive Testing

Recursive from Imports May Not Work

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

Test Your Knowledge: Part V Exercises

VI. Classes and OOP

25. OOP: The Big Picture

Why Use Classes?

OOP from 30,000 Feet

Attribute Inheritance Search

Classes and Instances

Class Method Calls

Coding Class Trees

OOP Is About Code Reuse

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

26. Class Coding Basics

Classes Generate Multiple Instance Objects

Class Objects Provide Default Behavior

Instance Objects Are Concrete Items

A First Example

Classes Are Customized by Inheritance

A Second Example

Classes Are Attributes in Modules

Classes Can Intercept Python Operators

A Third Example

Why Use Operator Overloading?

The World’s Simplest Python Class

Classes Versus Dictionaries

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

27. A More Realistic Example

Step 1: Making Instances

Coding Constructors

Testing As You Go

Using Code Two Ways

Step 2: Adding Behavior Methods

Coding Methods

Step 3: Operator Overloading

Providing Print Displays

Step 4: Customizing Behavior by Subclassing

Coding Subclasses

Augmenting Methods: The Bad Way

Augmenting Methods: The Good Way

Polymorphism in Action

Inherit, Customize, and Extend

OOP: The Big Idea

Step 5: Customizing Constructors, Too

OOP Is Simpler Than You May Think

Other Ways to Combine Classes

Step 6: Using Introspection Tools

Special Class Attributes

A Generic Display Tool

Instance Versus Class Attributes

Name Considerations in Tool Classes

Our Classes’ Final Form

Step 7 (Final): Storing Objects in a Database

Pickles and Shelves

Storing Objects on a Shelve Database

Exploring Shelves Interactively

Updating Objects on

Return Main Page Previous Page Next Page

®Online Book Reader