Skip to content
  • Sign In
  • Try Now
View all events
Python

Python Decorators

Published by O'Reilly Media, Inc.

Intermediate content levelIntermediate

How to Make Decorators and Use their Str@nge Power

In this course, you’ll:

  • Understand decorator syntax
  • Learn common decorator uses
  • Get hands-on with decorators

Ever seen a line like “@something” before a function or class definition in Python? That @ syntax uses a decorator to augment the behavior of the function or class that you’re defining. Decorators are a strange but powerful Python feature. Decorators wrap around functions and classes, dramatically augmenting their behavior in just one line of code. Decorators can validate function arguments, process return values, log exceptions, and more.

Join expert Trey Hunner to learn how decorators work, when and how to use them, and how to make your own decorators.

What you’ll learn and how you can apply it

  • Better utilize decorators built by others
  • Identify opportunities to use a decorator
  • Make your own decorators

This live event is for you because...

  • You’re an experienced Python developer.
  • You occasionally see an “@” symbol in Python and you’re not sure what it means.
  • You want to better understand when decorators are useful and how to make your own.

Prerequisites

  • Create a directory to use for this workshop in your home directory or desktop (or wherever you want to save your work). This folder will be referred to as "decorators" in this course, but you can name it whatever you'd like. Just don't forget where you put it.
  • Download the ZIP file of course exercises and extract it into the decorators directory you just made.
  • When unzipped, that ZIP file will create a new folder called "exercises." It contains the test framework you’ll be using as well as the files you’ll need to modify for most of the exercises in the workshop.
  • Open up your system command prompt or terminal (not a Python prompt) and change directories to that exercises directory. On Windows you may need to type "C:" or another drive letter to change drives first. There are a couple shortcuts in Windows for opening a command prompt in the correct directory quickly (see here and here for examples).
  • Once in the exercises directory, open a command prompt or terminal and run "py -3 test.py is_ok" (Windows) or "python3 test.py is_ok" (Linux/macOS). You should see a success message that includes "Congrats and welcome to the Test Framework!" and ends with "OK".

Recommended follow-up:

Schedule

The time frames are only estimates and may vary according to how the class is progressing.

Common decorators (30 minutes)

  • Presentation: Common decorator uses; useful decorators in Python and third-party libraries
  • Hands-on exercises: Use a decorator
  • Q&A

First-class functions (45 minutes)

  • Presentation: Function objects can be passed around; functions can be defined within functions
  • Hands-on exercises: Explore function objects
  • Q&A

Decorators: The basics (45 minutes)

  • Presentation: The decorator syntax
  • Hands-on exercises: Dive into decorators
  • Break
  • Q&A

Deeper into decorators (60 minutes)

  • Presentation: Well-behaved decorators, context decorators, class decorators, and decorators that accept arguments
  • Hands-on exercises: Dive deeper into decorators
  • Q&A

Your Instructor

  • Trey Hunner

    Trey Hunner helps Python programmers level up their skills through corporate training and through Python Morsels, a Python skill-building service for individuals and small teams. Trey also sends out a new Python tip every week through his email newsletter at pym.dev/newsletter.

    linkedinXlinksearch