Meta-Programming in Python

A short tutorial on decorators and meta-classes

Saurabh Kukade
Oct 15, 2019 · 4 min read

Recently, I encountered a very fascinating concept which is meta-programming in Python. I would like to share my findings on this topic in this article. I hope that it may help you to wrap your head around this because they say it is a tough nut to crack.

What Is Meta-Programming?

So, in one line: “Meta-programming is an act of writing code that manipulates code.”

Wait, what? Yes, you read it right. Code that manipulates code. Doesn’t it sound fascinating and powerful? Well, actually it is.

In the context of Python, meta-programming can be stated as: “Meta-programming is an act of building functions and classes that can manipulate code by modifying, wrapping existing code, or generating code.”

Meta-programming in Python can be achieved with:

  1. Decorators
  2. Meta-classes

Let’s get familiar with them one-by-one.


A decorator is a way of adding new functionality to an existing function without modifying its original structure.

For instance, we have these three functions:

Now we need to print the function name and parameter values when the function gets called. This should be applicable to all three functions above.

The native way is to add print/log statements to all three functions. But this sounds like very repetitive work and we’d also need to modify each function body.

Can we do better? Of course we can, because by the grace of God, we are programmers and programmers are intelligent. We can achieve this by writing a decorator function and by not modifying any of the existing function body.

Bingo! In the above code snippet, is a decorator function. We decorate all three functions with and we have not touched the existing function body to add this print functionality.

So, basically, decorators are higher-order functions that take a function as an argument and returns another function. Here, takes a function as an argument and returns as a result, where adds our print functionality to .

There is more to decorators but this is a brief introduction to decorators in Python.


Now that we’ve seen decorators, they are for decorating functions. But there is more to meta-programming than decorators, such as meta-classes.

Meta-classes are special types of classes, rather than ordinary classes in Python. Where an ordinary class defines behavior of its own instance, a meta-class defines the behavior of an ordinary class and its instance.

A meta-class can add or subtract a method or field to an ordinary class. Python has one special class, the class, which is by default a meta-class. All custom type classes must inherit from the class.

For instance, if we have class , with three class methods, and we want to provide debug functionality to all the methods in one class then we can use a meta-class for this.

First, we need to create a meta-class , with debug functionality, and make the class inherit from .

And, when we call any method from the class, it will get invoked with our .

Bingo! In the above snippet, we created a meta-class and wrote a new method which is responsible for creating an instance of class and applies our decorator function to the object (instance), which will get created for every class that inherits .

is inherited from , hence every method has been decorated by from .

This way, we can add new behavior to all the methods within a class and also control the instance creation of a class using a meta-class. We can achieve a lot with a meta-class, such as adding a method or field to class, removing a method or field from a class, and many more.

I wanted you to have a quick look at meta-programming in Python, so I wasn’t able to cover all the things in this post.

I hope that this article has helped you to familiarize yourself with the concept of meta-programming. Criticism is always welcome!

Better Programming

Advice for programmers.

Saurabh Kukade

Written by

Trying to see the world through Haskell. Software Developer. Interested in functional programming paradigm.

Better Programming

Advice for programmers.

More From Medium

More from Better Programming

More from Better Programming

More from Better Programming

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade