Making Open Work
May 8–9, 2017: Training & Tutorials
May 10–11, 2017: Conference
Austin, TX

Mastering Python decorators

Aaron Maxwell (Powerful Python)
9:00am12:30pm Monday, May 8, 2017
Adopt This Now
Location: Ballroom E
Level: Intermediate
Average rating: ****.
(4.00, 5 ratings)

Who is this presentation for?

  • Software engineers creating libraries and frameworks, QA engineers sick and tired of repetitive testing code, data scientists who want to leverage more powerful Python abstractions, and anyone who wants to understand how popular Python libraries leverage decorators

Prerequisite knowledge

  • Substantial Python experience (You must fully understand the basics of writing and using functions, OOP in Python, and Python's built-in data structures—list, dict, tuple, etc.)
  • A working knowledge of a Python-aware IDE
  • Comfort treating functions as first-class objects—passing a function to another function as an argument, writing functions that return other functions, and so on (We will briefly review the simple Python syntax for this, so it's sufficient if you have encountered the basic idea in JavaScript, Java 8 Lambdas, Scala, Lisp/Scheme/Clojure, etc.)

Materials or downloads needed in advance

  • A laptop with either Python 2.7 or Python 3.5 or later and a Python-aware IDE that you have mastered for day-to-day Python programming work installed (You must be able to execute Python programs either in the IDE or on the command line.)

What you'll learn

  • Learn to write Python decorators
  • Explore Pythonic metaprogramming and expressive Python library design

Description

Python sports a powerful, mysterious feature called the decorator. Decorators add rich behavior to groups of functions and classes without modifying them at all, untangle distinct, frustratingly intertwined concerns in your code in ways simply not possible with functions and classes, and build powerful, extensible software frameworks other developers love to use. Popular frameworks like Flask, Django, pytest, SQLAlchemy, Twisted, and even Python’s standard library all rely on decorators to provide key functionality.

But while decorators are trivially easy to use, they are far from easy to write. Creating your own decorators requires a number of advanced Python skills: a sophisticated understanding of Python’s memory model, familiarity with uncommon abstractions of functions, dexterity with generic programming, and more. Most Python developers miss out on the benefits of decorators because they never learn to write them.

Aaron Maxwell teaches you how to write compelling and useful decorators other developers will flock to use. Come ready to code. You’ll leave with fluent mastery in writing and leveraging Python decorators.

Topics include:

  • Python foundations: Function objects, variable arguments, and argument unpacking
  • The basic decorator pattern
  • Data encapsulation in decorators
  • Different decorator patterns and anti-patterns
  • Decorators taking arguments
  • Class-based decorator implementation (to leverage inheritance hierarchies, etc.)
  • Preserving wrapped function characteristics and other decorator gotchas
Photo of Aaron Maxwell

Aaron Maxwell

Powerful Python

Aaron Maxwell trains working engineers in advanced Python development and DevOps.