Metaclasses - When to Use and When Not to Use

Mike Müller (@pyacademy)

Mike Müller has been using Python as his primary programming language since 1999. He is a Python trainer and the CEO at Python Academy (www.python-academy.com).

He teaches a wide variety of Python topics including "Introduction to Python", "Python for Scientists and Engineers", "Advanced Python" as well as "Optimization and Extensions of Python Programs".

He is the chairman of the Python Software Verband e.V., a PSF fellow, a PSF community service award holder, User Group co-founder. He chaired EuroSciPy 2008 and 2009, PyCon DE 2011 and 2012 as well as EuroPython 2014 in Berlin, Germany.

Abstract

Most Python programmer will never use metaclasses in production code. Nevertheless, it can be useful to learn how they work to get a deeper understanding of Python internals. Furthermore, they can help to inspect a foreign code base at run time by programmatically extracting interesting details.

Description

Description

Metaclasses are an advanced Python feature. While it is possible to write Python programs without active knowledge of them, knowing more about them facilitates a deeper understanding of the language. With examples, you will learn how they work and how to write your own metaclasses. Furthermore, you will understand when to use and when better not to use them.

This tutorial is a systematic introduction to metaclasses. It covers all relevant information with a focus on practical applications for common tasks.

In hand-on sessions you will experience how metaclasses can help you to get more insight into a code base.

Use cases provide working code that can serve as a basis for your own solutions. You will gain a deeper understanding of more advanced concepts that can help to write better programs.

I've been delivering these topics over the last years as a part of an advanced training in open and in-house courses as well as trainings at PyCon US, EuroPython, PyCon PL, PyCon DE and PyCon IE. The material has been continuously refined owing to the participant feedback.

Software Requirements

You will need Python 2.7 or 3.6 installed on your laptop. Python 2.6 or 3.4/3.5 should also work. Python 3.x is strongly preferred.

Jupyter Notebook

I will use a Jupyter Notebook for the tutorial because it makes a very good teaching tool. You are welcome to use the setup you prefer, i.e editor, IDE, REPL. If you also like to use a Jupyter Notebook, I recommend conda for easy installation. Similarly to virtualenv, conda allows creating isolated environments but allows binary installs for all platforms.

There are two ways to install Jupyter via conda:

  1. Use Minconda. This is a small install and (after you installed it) you can use the command conda to create an environment: conda create -n pycon2016 python=3.5 Now you can change into this environment: source activate pycon2016. The prompt should change to (pycon2017). Now you can install IPython: conda install Jupyter.

  2. Install Anaconda and you are ready to go if you don't mind installing lots of packages from the scientific field.

Working witch conda environments

After creating a new environment, the system might still work with some stale settings. Even when the command which tells you that you are using an executable from your environment, this might actually not be the case. If you see strange behavior using a command line tool in your environment, use hash -r and try again.

Audience

Intermediate Python programmers who want to dive deeper into how Python works.

Participants will learn how metaclasses work and what they are good for. Emphasis is on when to use and not to use them to keep programs as simple as appropriate but at the same time explore the power of Python where it really pays off.

Outline

  • Why Metaclasses?
  • What is a Metaclass?
  • Working with __new__ and __init__
  • Use Case Classes with Methods Only
  • Use Case Working with Slots
  • Use Case Counting the Number of Class Definitions
  • Metaclasses vs. Class Decorators
  • Metaclasses in the Wild - Use Cases in Django and SQLAlchemy
  • Exercises