Skip to main content

Full Monty: Intro to Python Metaprogramming

Luciano Ramalho (ThoughtWorks)
Tutorial Please note: to attend, your registration must include Tutorials.
Average rating: ***..
(3.75, 12 ratings)
Slides:   1-PDF 


If you look at Python itself as a framework, its core objects are the components at your service and the Data Model defines the interfaces to which you implement in your own objects. It describes common APIs for the core types in the language.

Functions, modules and classes are first class objects: they can be created, examined and modified dynamically. In particular, decorators allow customizing function and class definitions and metaclasses provide fine-grained control of class behavior. That is how frameworks from Django to Kivy do their magic.

The special methods defined in the Data Model are called by the Python interpreter to handle your objects in a variety of contexts like:

  • iteration;
  • acessing items in collections using [];
  • attribute access using obj.attr;
  • function and method invocation;
  • operator overloading;
  • string representation and formatting;
  • object creation and destruction;
  • class building;
  • managed contexts (i.e. with blocks);

This tutorial uses TDD (Test Driven Design) to present and solve a series of problems that can be elegantly solved with special methods. The examples and exercises include:

  • iterating over words in text;
  • simple math with multi-dimensional vectors;
  • dynamic attributes to implement the Proxy design pattern;
  • using descriptors and metaclasses or class decorators enforce business rules;

Most of the techniques apply from Python 2.6 to 3.4. When there are differences, they will be noted.


* Participants should have some experience coding in Python, or at least understand the topics covered in the official Python Tutorial.
* Attendees should bring laptops with Python ≥ 3.3 installed.

QUESTIONS for the speaker?: Use the “Leave a Comment or Question” section at the bottom to address them.

Photo of Luciano Ramalho

Luciano Ramalho


Luciano Ramalho was a Web developer before the Netscape IPO in 1995, and switched from Perl to Java to Python in 1998. Since then he worked on some of the largest news portals in Brazil using Python, and taught Python web development in the Brazilian media, banking and government sectors. His speaking credentials include OSCON 2013 (slides) and 2002, two talks at PyCon USA 2013 and 17 talks over the years at PythonBrasil (the Brazilian PyCon), FISL (the largest FLOSS conference in the Southern Hemisphere) and a keynote at the RuPy Strongly Dynamic Conference in Brazil. Ramalho is a member of the Python Software Foundation and co-founder of Garoa Hacker Clube, the first hackerspace in Brazil. He is a managing partner at, a training company.