Metaclasses – When to Use and When Not to Use
The lettering »Open Codes« in magenta against a background of code.
Fri, October 27, 2017 10.30 am CEST

As part of the »PyCon.DE 2017 & PyData Karlsruhe conference«, visitors of the »Open Codes« exhibition are invited to attend the workshop »Metaclasses – When to Use and When Not to Use« with Python trainer and CEO at Python Academy Mike Müller. 

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.

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.

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

  • a Jupyter Notebook will be used 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, conda is recommended for easy installation. Similarly to virtualenv, conda allows creating isolated environments but allows binary installs for all platforms

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

Accompanying program

In cooperation with