Wednesday, 4 November 2009

My Pycon talk (Python Metaprogramming)

If you followed the link on my previous post and looked at #64 you might have noticed I'll be giving a talk on Python Metaprogramming. Here's a sneak peak at what the talk will be like along with some comments.


  • Metaprogramming:
    A short explanation of what metaprogramming is, why it is needed and how it can save you time and effort.

  • Metaprogramming Languages:

    What languages are considered "metaprogramming languages" and what constructs do they provide to be considered as such. A 30000 ft view exploration of the metaprogramming features of languages like Lisp, Ruby, Perl, Javascript, Haskell and OCaml.

  • Cool uses/examples of metaprogramming in other languages:

    Some interesting examples of metaprogramming regardless of the language. These examples will be ported to Python at the end of the talk. I will go through this very fast just to show what we want to achieve in each example and what techniques are used by other programming languages to achieve this effects. A more detailed explanation will be done at the end of the talk by porting the examples to Python.

  • Tools for metaprogramming in Python:

    Each of the subsections of "Tools for metaprogramming in Python" should contain the following information:

    • Explaination: short explaination

    • Short example in Python

    • An example of using the technique to port some othe language metaprogramming features

    • Patterns: Common or useful patterns to incorporate that metaprogramming tool into your code

    • Advantages, disadvantages and perils.
The subsections I'll be covering are the following:
  1. Code generation: eval() and exec():
    The basics. Code Generation as strings and getting them to execute.

  2. Python (A.K.A.: Python's data model):
    Exploring the flexibility of Python's data model: Duck-typing, special methods, attribute access, callable emulation, behaviour on operators.

  3. Decorators:
    An introduction to decoratos and what can be achieved with them. Emphasis on decorator patterns.

  4. Metaclasses:
    A basic introduction to metaclasses: What they are and when are they needed. Includes an idea of when can metaclasses be replaced by decorators.

  5. types. Specially types.FunctionType:
    Exploring type objcts. Specially function objects, code attributes, locals, globals defaults and closures

  6. Others: import hooks, ByteCode, etc:
    A seccion on other useful metaprogramming tools that might fit into the time frame of the talk. (this seccion might be included depending on the time or if there are in depth talks in pycon about somme of the above techniques that suggest cutting down on them in favour of other options)

  • Putting it all together:
    Just a pointer to a resourse that shows example code using most of the techniques shown throughout the talk. The example will probably be creating a DSL: possibly language generation (HTML, CSS, SQL or other), a generic problem solver or a small framework for distributed computation. I might take a lightning talk to go through this example in more detail.

  • The Good, the Bad and the Ugly:
    A short conclusion about metaprogramming in Python. Why use it, when to use it, when to avoid it and what is python missing regarding metaprogramming that other languages have.

  • Alternative titles

    There are some alternative titles I'm considering for the talk. Any favourites?
    • Baddass Metaprogramming with Python (Thanks, Ahsanul)
    • The swiss army knife for metaprogramming in Python
    • Metaprogramming tricks for Python programmers
    • A zoo of Python metaprogramming tools
    • Tools and techniques for Python metaprogramming
    • Enhancing your code through metaprogramming
    • Python metaprogramming tricks for your toolbox


    1. Call it "Badass Metaprogramming with Python"

    2. haha, love it! I'm adding it to the title options :)