Skip to content

目录

1 Introduction

  • 1.1 What’s a Python Trick?
  • 1.2 What This Book Will Do for You
  • 1.3 How to Read This Book

2 Patterns for Cleaner Python

  • 2.1 Covering Your A** With Assertions
  • 2.2 Complacent Comma Placement
  • 2.3 Context Managers and the with Statement
  • 2.4 Underscores, Dunders, and More
  • 2.5 A Shocking Truth About String Formatting
  • 2.6 “The Zen of Python” Easter Egg

3 Effective Functions

  • 3.1 Python’s Functions Are First-Class
  • 3.2 Lambdas Are Single-Expression Functions
  • 3.3 The Power of Decorators
  • 3.4 Fun With args and *kwargs
  • 3.5 Function Argument Unpacking
  • 3.6 Nothing to Return Here

4 Classes & OOP

  • 4.1 Object Comparisons: “is” vs “==”
  • 4.2 String Conversion (Every Class Needs a __repr__)
  • 4.3 Defining Your Own Exception Classes
  • 4.4 Cloning Objects for Fun and Profit
  • 4.5 Abstract Base Classes Keep Inheritance in Check
  • 4.6 What Namedtuples Are Good For
  • 4.7 Class vs Instance Variable Pitfalls
  • 4.8 Instance, Class, and Static Methods Demystified

5 Common Data Structures in Python

  • 5.1 Dictionaries, Maps, and Hashtables
  • 5.2 Array Data Structures
  • 5.3 Records, Structs, and Data Transfer Objects
  • 5.4 Sets and Multisets
  • 5.5 Stacks (LIFOs)
  • 5.6 Queues (FIFOs)
  • 5.7 Priority Queues

6 Looping & Iteration

  • 6.1 Writing Pythonic Loops
  • 6.2 Comprehending Comprehensions
  • 6.3 List Slicing Tricks and the Sushi Operator
  • 6.4 Beautiful Iterators
  • 6.5 Generators Are Simplified Iterators
  • 6.6 Generator Expressions
  • 6.7 Iterator Chains

7 Dictionary Tricks

  • 7.1 Dictionary Default Values
  • 7.2 Sorting Dictionaries for Fun and Profit
  • 7.3 Emulating Switch/Case Statements With Dicts
  • 7.4 The Craziest Dict Expression in the West
  • 7.5 So Many Ways to Merge Dictionaries
  • 7.6 Dictionary Pretty-Printing

8 Pythonic Productivity Techniques

  • 8.1 Exploring Python Modules and Objects
  • 8.2 Isolating Project Dependencies With Virtualenv
  • 8.3 Peeking Behind the Bytecode Curtain

9 Closing Thoughts

  • 9.1 Free Weekly Tips for Python Developers
  • 9.2 PythonistaCafe: A Community for Python Developers