Skip to main content

Full Monty: Intro to Python Metaprogramming

Luciano Ramalho (Python.pro.br)
Python
E147/148
Tutorial Please note: to attend, your registration must include Tutorials.
Average rating: ***..
(3.82, 11 ratings)
Slides:   1-PDF 

THIS TUTORIAL HAS REQUIREMENTS AND INSTRUCTIONS LISTED BELOW

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.

TUTORIAL REQUIREMENTS AND INSTRUCTIONS FOR ATTENDEES

* 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

Python.pro.br

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 Python.pro.br, a training company.

Leave a Comment or Question

Help us make this conference the best it can be for you. Have questions you'd like this speaker to address? Suggestions for issues that deserve extra attention? Feedback that you'd like to share with the speaker and other attendees?

Join the conversation here (requires login)