Learning Python - Mark Lutz [2]
Changing Lists: range
Parallel Traversals: zip and map
Generating Both Offsets and Items: enumerate
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
14. Iterations and Comprehensions, Part 1
Iterators: A First Look
The Iteration Protocol: File Iterators
Manual Iteration: iter and next
Other Built-in Type Iterators
List Comprehensions: A First Look
List Comprehension Basics
Using List Comprehensions on Files
Extended List Comprehension Syntax
Other Iteration Contexts
New Iterables in Python 3.0
The range Iterator
The map, zip, and filter Iterators
Multiple Versus Single Iterators
Dictionary View Iterators
Other Iterator Topics
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
15. The Documentation Interlude
Python Documentation Sources
# Comments
The dir Function
Docstrings: __doc__
PyDoc: The help Function
PyDoc: HTML Reports
The Standard Manual Set
Web Resources
Published Books
Common Coding Gotchas
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
Test Your Knowledge: Part III Exercises
IV. Functions
16. Function Basics
Why Use Functions?
Coding Functions
def Statements
def Executes at Runtime
A First Example: Definitions and Calls
Definition
Calls
Polymorphism in Python
A Second Example: Intersecting Sequences
Definition
Calls
Polymorphism Revisited
Local Variables
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
17. Scopes
Python Scope Basics
Scope Rules
Name Resolution: The LEGB Rule
Scope Example
The Built-in Scope
The global Statement
Minimize Global Variables
Minimize Cross-File Changes
Other Ways to Access Globals
Scopes and Nested Functions
Nested Scope Details
Nested Scope Examples
The nonlocal Statement
nonlocal Basics
nonlocal in Action
Why nonlocal?
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
18. Arguments
Argument-Passing Basics
Arguments and Shared References
Avoiding Mutable Argument Changes
Simulating Output Parameters
Special Argument-Matching Modes
The Basics
Matching Syntax
The Gritty Details
Keyword and Default Examples
Arbitrary Arguments Examples
Python 3.0 Keyword-Only Arguments
The min Wakeup Call!
Full Credit
Bonus Points
The Punch Line...
Generalized Set Functions
Emulating the Python 3.0 print Function
Using Keyword-Only Arguments
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
19. Advanced Function Topics
Function Design Concepts
Recursive Functions
Summation with Recursion
Coding Alternatives
Loop Statements Versus Recursion
Handling Arbitrary Structures
Function Objects: Attributes and Annotations
Indirect Function Calls
Function Introspection
Function Attributes
Function Annotations in 3.0
Anonymous Functions: lambda
lambda Basics
Why Use lambda?
How (Not) to Obfuscate Your Python Code
Nested lambdas and Scopes
Mapping Functions over Sequences: map
Functional Programming Tools: filter and reduce
Chapter Summary
Test Your Knowledge: Quiz
Test Your Knowledge: Answers
20. Iterations and Comprehensions, Part 2
List Comprehensions Revisited: Functional Tools
List Comprehensions Versus map
Adding Tests and Nested Loops: filter
List Comprehensions and Matrixes
Comprehending List Comprehensions
Iterators Revisited: Generators
Generator Functions: yield Versus return
Generator Expressions: Iterators Meet Comprehensions
Generator Functions Versus Generator Expressions
Generators Are Single-Iterator Objects
Emulating zip and map with Iteration Tools
Value Generation in Built-in Types and Classes
3.0 Comprehension Syntax Summary
Comprehending Set and Dictionary Comprehensions
Extended Comprehension Syntax for Sets and Dictionaries
Timing Iteration Alternatives
Timing Module
Timing Script
Timing Results
Timing Module Alternatives
Other Suggestions
Function Gotchas
Local Names Are Detected Statically
Defaults and Mutable Objects
Functions Without returns