PyCon: Python Metaprogramming for Mad Scientists and Evil Geniuses

See the website.

This was one of the best talks.

Python is ideal for mad scientists (because it's cool) and evil geniuses (because it has practical applications).

  • Synthetic functions, classes, and modules
  • Monkey patching
"Synthetic" means building something without the normally required Python source code.

Synthetic functions can be created using exec.

Synthetic classes can be created using type('name', (), d).

(exec and eval are very popular at PyCon this year. Three talks have shown good uses for them. I wonder if this is partially inspired by Ruby.)

Here's how to create a synthetic module:
import new
module = new.module(...)
sys.modules['name'] = module
Functions, classes, and modules are just objects in memory.

Patching third-party objects is more robust than patching third-party code.

You can use these tricks to implement Aspect-Oriented Programming.

(I wonder if it's possible to implement "call by name" using the dis module and messing with the caller's frame.)

You might need to synthesize a replacement module if you need to replace a module written in C.

Monkeypatching a class is tricker:
MyClass.spam = new.instancemethod(new_spam, None, MyClass)
obj.spam = new.instancemethod(new_spam, obj, MyClass)
The room was packed. This was a very popular talk.

This is how to "fix" executables:
  • Create a special version of containing your fix.
  • Create a wrapper script for the executable that sets PYTHONPATH to contain the dir containing
People should use "#!/usr/bin/env python" so that the caller can control which Python it uses.

He monkeypatched __import__ so that code gets executed whenever someone tries to import something! Wow! I've never seen that trick before! gets invoked really early. It gets imported so early, it can be an awkward environment to try to work in. For instance, sys.argv doesn't even exist yet.

He showed a bunch of good monkeypatching examples. For instance, he monkeypatched the pwd module.

It's okay if code breaks, as long as it breaks early and loudly.

You can shove anything in sys.modules as long as it responds to __getattr__. You can even synthesize behavior based on dynamic dispatch.

It's easy to get into a situation where things don't even make sense anymore for other people debugging your code.

If you're going to patch third-party code:
  • Do it seldom.
  • Do it publicly.
New releases of third-party code can still break a monkey patch.

More evil genius tools:Generating code is good because you'll get line numbers.

Using the ast module is another approach.

"new" is deprecated (in Python 3, I think). It's been replaced by the "types" module.