How decorators function in Python

Abstract

Have you ever seen those “@” tags on top of Python functions and classes? Those are called decorators. Decorators, in their simplest form, wrap functions around functions. That might sound confusing at first, but it’s actually pretty useful. A simple decorator could measure execution times, add startup steps, or automatically repeat calls. Decorators are one of Python’s niftiest language features, and they help programmers write DRY (Don’t Repeat Yourself) code.

In this talk, we’ll learn all about decorators:

  • How they wrap functions
  • How to write our own decorators
  • How to do cool tricks with arguments, classes, and nesting
  • How to use popular decorators
  • How to decide when decorators are (and aren’t) the right solution

We’ll walk through plenty of example code together. We’ll also touch lightly on Functional Programming (FP) and Aspect-Oriented Programming (AOP) concepts to build a firm understanding about how decorators work. After this talk, you should be able to use decorators effectively in your own Python projects!

About the speaker

guide
Andy Knight
Lead Software Engineer, Q2

Andrew Knight is the "Automation Panda" - a software engineering leader who builds solutions for software testing problems. Over the past decade, he has designed and built robust test automation projects from the ground up that can run thousands of tests continuously. He is the author of the upcoming book "The Way To Test Software" from Manning Publications, and he is also the lead developer for Boa Constrictor, the .NET Screenplay Pattern. Andy currently works full-time as the Lead Software Engineer in Test for PrecisionLender at Q2. Read his tech blog at AutomationPanda.com, and follow him on Twitter at @AutomationPanda.