You are previewing Pro Python, Second Edition.
O'Reilly logo
Pro Python, Second Edition

Book Description

You’ve learned the basics of Python, but how do you take your skills to the next stage? Even if you know enough to be productive, there are a number of features that can take you to the next level in Python. Pro Python, Second Edition explores concepts and features normally left to experimentation, allowing you to be even more productive and creative.

In addition to pure code concerns, Pro Python develops your programming techniques and approaches, which will help make you a better Python programmer. This book will improve not only your code but also your understanding and interaction with the many established Python communities.

This book takes your Python knowledge and coding skills to the next level. It shows you how to write clean, innovative code that will be respected by your peers. With this book, make your code do more with introspection and meta-programming. And learn and later use the nuts and bolts of an application, tier-by-tier as a complex case study along the way.

For more information, including a link to the source code referenced in the book, please visit http://propython.com/.

Table of Contents

  1. Cover
  2. Title
  3. Copyright
  4. Contents at a Glance
  5. Contents
  6. About the Authors
  7. About the Technical Reviewer
  8. Acknowledgments
  9. Introduction
  10. Chapter 1: Principles and Philosophy
    1. The Zen of Python
      1. Beautiful Is Better Than Ugly
      2. Explicit Is Better Than Implicit
      3. Simple Is Better Than Complex
      4. Complex Is Better Than Complicated
      5. Flat Is Better Than Nested
      6. Sparse Is Better Than Dense
      7. Readability Counts
      8. Special Cases Aren’t Special Enough to Break the Rules
      9. Although Practicality Beats Purity
      10. Errors Should Never Pass Silently
      11. Unless Explicitly Silenced
      12. In the Face of Ambiguity, Refuse the Temptation to Guess
      13. There Should Be One—and Preferably Only One—Obvious Way to Do It
      14. Although That Way May Not Be Obvious at First Unless You’re Dutch
      15. Now Is Better Than Never
      16. Although Never Is Often Better Than Right Now
      17. If the Implementation Is Hard to Explain, It’s a Bad Idea
      18. If the Implementation Is Easy to Explain, It May Be a Good Idea
      19. Namespaces Are One Honking Great Idea—Let’s Do More of Those!
    2. Don’t Repeat Yourself
    3. Loose Coupling
    4. The Samurai Principle
    5. The Pareto Principle
    6. The Robustness Principle
    7. Backward Compatibility
    8. The Road to Python 3.0
    9. Taking It With You
  11. Chapter 2: Advanced Basics
    1. General Concepts
      1. Iteration
      2. Caching
      3. Transparency
    2. Control Flow
      1. Catching Exceptions
      2. Exception Chains
      3. When Everything Goes Right
      4. Proceeding Regardless of Exceptions
      5. Optimizing Loops
      6. The with Statement
      7. Conditional Expressions
    3. Iteration
      1. Sequence Unpacking
      2. List Comprehensions
      3. Generator Expressions
      4. Set Comprehensions
      5. Dictionary Comprehensions
      6. Chaining Iterables Together
      7. Zipping Iterables Together
    4. Collections
      1. Sets
      2. Named Tuples
      3. Ordered Dictionaries
      4. Dictionaries with Defaults
    5. Importing Code
      1. Fallback Imports
      2. Importing from the Future
      3. Using __all__ to Customize Imports
      4. Relative Imports
      5. The __import__( ) function
      6. The importlib module
    6. Taking It With You
  12. Chapter 3: Functions
    1. Arguments
      1. Planning for Flexibility
      2. Variable Positional Arguments
      3. Variable Keyword Arguments
      4. Combining Different Kinds of Arguments
      5. Invoking Functions with Variable Arguments
      6. Preloading Arguments
      7. Introspection
      8. Example: Identifying Argument Values
      9. Example: A More Concise Version
      10. Example: Validating Arguments
    2. Decorators
      1. Closures
      2. Wrappers
      3. Decorators with Arguments
      4. Decorators with—or without—Arguments
      5. Example: Memoization
      6. Example: A Decorator to Create Decorators
    3. Function Annotations
      1. Example: Type Safety
      2. Factoring Out the Boilerplate
      3. Example: Type Coercion
      4. Annotating with Decorators
      5. Example: Type Safety as a Decorator
    4. Generators
    5. Lambdas
    6. Introspection
      1. Identifying Object Types
      2. Modules and Packages
      3. Docstrings
    7. Taking It with You
  13. Chapter 4: Classes
    1. Inheritance
      1. Multiple Inheritance
      2. Method Resolution Order
      3. Example: C3 Algorithm
      4. Using super() to Pass Control to Other Classes
      5. Introspection
    2. How Classes Are Created
      1. Creating Classes at Runtime
      2. Metaclasses
      3. Example: Plugin Framework
      4. Controlling the Namespace
    3. Attributes
      1. Properties
      2. Descriptors
    4. Methods
      1. Unbound Methods
      2. Bound Methods
      3. Class Methods
      4. Static Methods
      5. Assigning Functions to Classes and Instances
    5. Magic Methods
      1. Creating Instances
      2. Example: Automatic Subclasses
      3. Dealing with Attributes
      4. String Representations
    6. Taking It With You
  14. Chapter 5: Common Protocols
    1. Basic Operations
      1. Mathematical Operations
      2. Bitwise Operations
      3. Variations
    2. Numbers
      1. Sign Operations
      2. Comparison Operations
    3. Iterables
      1. Example: Repeatable Generators
    4. Sequences
    5. Mappings
    6. Callables
    7. Context Managers
    8. Taking It With You
  15. Chapter 6: Object Management
    1. Namespace Dictionary
      1. Example: Borg Pattern
      2. Example: Self-Caching Properties
    2. Garbage Collection
      1. Reference Counting
      2. Cyclical References
      3. Weak References
    3. Pickling
    4. Copying
      1. Shallow Copies
      2. Deep Copies
    5. Taking It With You
  16. Chapter 7: Strings
    1. Bytes
      1. Simple Conversion: chr( ) and ord( )
      2. Complex Conversion: The Struct Module
    2. Text
      1. Unicode
      2. Encodings
    3. Simple Substitution
    4. Formatting
      1. Looking Up Values Within Objects
      2. Distinguishing Types of Strings
      3. Standard Format Specification
      4. Example: Plain Text Table of Contents
      5. Custom Format Specification
    5. Taking It With You
  17. Chapter 8: Documentation
    1. Proper Naming
    2. Comments
    3. Docstrings
      1. Describe What the Function Does
      2. Explain the Arguments
      3. Don’t Forget the Return Value
      4. Include Any Expected Exceptions
    4. Documentation Outside the Code
      1. Installation and Configuration
      2. Tutorials
      3. Reference Documents
    5. Documentation Utilities
      1. Formatting
      2. Links
      3. Sphinx
    6. Taking It With You
  18. Chapter 9: Testing
    1. Test-Driven Development
    2. Doctests
      1. Formatting Code
      2. Representing Output
      3. Integrating With Documentation
      4. Running Tests
    3. The unittest Module
      1. Setting Up
      2. Writing Tests
      3. Other Comparisons
      4. Testing Strings and Other Sequence Content
      5. Testing Exceptions
      6. Testing Identity
      7. Tearing Down
    4. Providing a Custom Test Class
      1. Changing Test Behavior
    5. Taking It With You
  19. Chapter 10: Distribution
    1. Licensing
      1. GNU General Public License
      2. Affero General Public License
      3. GNU Lesser General Public License
      4. Berkeley Software Distribution License
      5. Other Licenses
    2. Packaging
      1. setup.py
      2. MANIFEST.in
      3. The sdist Command
    3. Distribution
    4. Taking It With You
  20. Chapter 11: Sheets: A CSV Framework
    1. Building a Declarative Framework
      1. Introducing Declarative Programming
      2. To Build or Not to Build?
    2. Building the Framework
      1. Managing Options
      2. Defining Fields
      3. Attaching a Field to a Class
      4. Adding a Metaclass
      5. Bringing It Together
    3. Ordering Fields
      1. DeclarativeMeta.__prepare__()
      2. Column.__init__()
      3. Column.__new__()
      4. CounterMeta.__call__()
      5. Choosing an Option
    4. Building a Field Library
      1. StringField
      2. IntegerColumn
      3. FloatColumn
      4. DecimalColumn
      5. DateColumn
    5. Getting Back to CSV
      1. Checking Arguments
      2. Populating Values
      3. The Reader
      4. The Writer
    6. Taking It With You
  21. Appendix A: Style Guide for Python
    1. Introduction
    2. A Foolish Consistency Is the Hobgoblin of Little Minds
    3. Code Layout
      1. Indentation
      2. Tabs or Spaces?
      3. Maximum Line Length
      4. Blank Lines
      5. Encodings (PEP 263)
    4. Imports
    5. Whitespace in Expressions and Statements
      1. Pet Peeves
      2. Other Recommendations
    6. Comments
      1. Block Comments
      2. Inline Comments
    7. Documentation Strings
    8. Version Bookkeeping
    9. Naming Conventions
      1. Descriptive: Naming Styles
      2. Prescriptive: Naming Conventions
    10. Programming Recommendations
    11. Copyright
  22. Appendix B: Voting Guidelines
    1. Abstract
    2. Rationale
    3. Voting Scores
    4. Copyright
  23. Appendix C: The Zen of Python
    1. Abstract
    2. The Zen of Python
    3. Easter Egg
    4. Copyright
  24. Appendix D: Docstring Conventions
    1. Abstract
    2. Rationale
    3. Specification
      1. What Is a Docstring?
      2. One-Line Docstrings
      3. Multiline Docstrings
      4. Handling Docstring Indentation
    4. Copyright
    5. Acknowledgments
  25. Appendix E: Backward Compatibility Policy
    1. Abstract
    2. Rationale
    3. Backward Compatibility Rules
    4. Making Incompatible Changes
    5. Copyright
  26. Appendix F: Python 3000
    1. Abstract
    2. Naming
    3. PEP Numbering
    4. Timeline
    5. Compatibility and Transition
    6. Implementation Language
    7. Meta-Contributions
    8. Copyright
  27. Appendix G: Python Language Moratorium
    1. Abstract
    2. Rationale
    3. Details
      1. Cannot Change
      2. Case-by-Case Exemptions
      3. Allowed to Change
    4. Retroactive
    5. Extensions
    6. Copyright
  28. Index