Skip to content

Python Tricks

Resources

Modules

Chapters Checklist

  • Patterns for Cleaner Python
    • Covering Your A** With Assertions
    • Complacent Comma Placement
    • Context Managers and the with Statement
    • Underscores, Dunders, and More
    • A Shocking Truth About String Formatting
    • “The Zen of Python” Easter Egg
  • Effective Functions
    • Python’s Functions Are First-Class
    • Lambdas Are Single-Expression Functions
    • The Power of Decorators
    • Fun With *args and **kwargs
    • Function Argument Unpacking
    • Nothing to Return Here
    • Object Comparisons: “is” vs “==”
    • String Conversion (Every Class Needs a __repr__)
    • Defining Your Own Exception Classes
    • Cloning Objects for Fun and Profit
    • Abstract Base Classes Keep Inheritance in Check
    • What Namedtuples Are Good For
    • Class vs Instance Variable Pitfalls
    • Instance, Class, and Static Methods Demystified
  • Common Data Structures in Python
    • Dictionaries, Maps, and Hashtables
    • Array Data Structures
    • Records, Structs, and Data Transfer Objects
    • Sets and Multisets
    • Stacks (LIFOs)
    • Queues (FIFOs)
    • Priority Queues
  • Looping & Iteration
    • Writing Pythonic Loops
    • Comprehending Comprehensions
    • List Slicing Tricks and the Sushi Operator
    • Beautiful Iterators
    • Generators Are Simplified Iterators
    • Generator Expressions
    • Iterator Chains
  • Dictionary Tricks
    • Dictionary Default Values
    • Sorting Dictionaries for Fun and Profit
    • Emulating Switch/Case Statements With Dicts
    • The Craziest Dict Expression in the West
    • So Many Ways to Merge Dictionaries
    • Dictionary Pretty-Printing
  • Pythonic Productivity Techniques
    • Exploring Python Modules and Objects
    • Isolating Project Dependencies With Virtualenv
    • Peeking Behind the Bytecode Curtain