How Decorators "decorate"

Aksh Gupta (~aksh)



Python developers have been using decorators for a number of use cases, but in most cases don't know how they're working behind the scenes. Decorators seem magical, but they really aren't. The peculiar @ syntax for a decorator is nothing but a syntactic sugar. But for what, you might ask? That's the question we'll answer together! How they actually work on the inside, what their structure is and how the control flows when we use decorators.

The talk will begin with a quick explanation of first-class functions and closures (since these will be required to understand the content). Further, we'll get into the meat of it, talking about how decorator functions are written with an enclosed wrapper function adding the extra functionality, the way they pass control flow and how they alter the actual function, and some generic decorators using *args and **kwars. We'll also look at the class-based implementation (by writing the __call__() method) of decorators, and how they're written differently from function-based decorators but still have a similar underlying concept. Finally, we'll also see how decorators that take arguments work, using some real-world examples (like Flask). The talk will show a number of pre-written code examples and their outputs for each of these concepts to clarify what the structure of a decorator function looks like, what its various parts do, and how all of it is finally executed.

Takeaways for the audience

Through this talk, my aim is to produce the same feeling in the audience that I felt when I first got to know of the working of decorators. It's a really simple, yet interesting process. They'll have a better understanding of what's actually happening to their original function whenever they use a decorator in future. Also, it'll help them write their own decorators, and appreciate the power that they offer.

The audience will also learn some handy basics of closures and first-class citizens.

Talk Outline and Flow

  • Intro: ~1 minute
  • Quick overview of first-class functions and closures: ~4 minutes
  • Going through a basic decorator function's structure and flow: 10 minutes
  • Class-based implementation of a decorator: 5 minutes
  • Decorators with arguments: ~3 minutes
  • Some practical cases of decorators that you might be using: 2 minutes
  • Wrapping up and Questions: 5 minutes


  • Basics of Python.
  • A basic idea of Closure functions is a plus (though I'll briefly go over them).

Video URL:

Speaker Info:

Aksh is a student pursuing Engineering, majoring in Information Technology. He's been working with Python since the last 4-5 years, and explored various domains. Recently, he's been exploring CLIs and system-level programming.

Community building and interaction really excite him, and he's also a part of his college's F/OSS society's core team, helping newcomers kickstatstart their OSS journeys. He's been giving talks in meetups at his college and loves to write (both technical and non-technical). Currently he's a technical writing intern at DeepSource, often working on some developer tools as well. He has also been involved in Open-Source organizations like Creative Commons (Project Collaborator) and MetaCall, and programs like the MLH Fellowship.

Section: Core Python
Type: Talks
Target Audience: Intermediate
Last Updated: