Skip to main content

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.


Popular posts from this blog

Ubuntu 20.04 on a 2015 15" MacBook Pro

I decided to give Ubuntu 20.04 a try on my 2015 15" MacBook Pro. I didn't actually install it; I just live booted from a USB thumb drive which was enough to try out everything I wanted. In summary, it's not perfect, and issues with my camera would prevent me from switching, but given the right hardware, I think it's a really viable option. The first thing I wanted to try was what would happen if I plugged in a non-HiDPI screen given that my laptop has a HiDPI screen. Without sub-pixel scaling, whatever scale rate I picked for one screen would apply to the other. However, once I turned on sub-pixel scaling, I was able to pick different scale rates for the internal and external displays. That looked ok. I tried plugging in and unplugging multiple times, and it didn't crash. I doubt it'd work with my Thunderbolt display at work, but it worked fine for my HDMI displays at home. I even plugged it into my TV, and it stuck to the 100% scaling I picked for the othe

ERNOS: Erlang Networked Operating System

I've been reading Dreaming in Code lately, and I really like it. If you're not a dreamer, you may safely skip the rest of this post ;) In Chapter 10, "Engineers and Artists", Alan Kay, John Backus, and Jaron Lanier really got me thinking. I've also been thinking a lot about Minix 3 , Erlang , and the original Lisp machine . The ideas are beginning to synthesize into something cohesive--more than just the sum of their parts. Now, I'm sure that many of these ideas have already been envisioned within , LLVM , Microsoft's Singularity project, or in some other place that I haven't managed to discover or fully read, but I'm going to blog them anyway. Rather than wax philosophical, let me just dump out some ideas: Start with Minix 3. It's a new microkernel, and it's meant for real use, unlike the original Minix. "This new OS is extremely small, with the part that runs in kernel mode under 4000 lines of executable code.&quo

Haskell or Erlang?

I've coded in both Erlang and Haskell. Erlang is practical, efficient, and useful. It's got a wonderful niche in the distributed world, and it has some real success stories such as CouchDB and Haskell is elegant and beautiful. It's been successful in various programming language competitions. I have some experience in both, but I'm thinking it's time to really commit to learning one of them on a professional level. They both have good books out now, and it's probably time I read one of those books cover to cover. My question is which? Back in 2000, Perl had established a real niche for systems administration, CGI, and text processing. The syntax wasn't exactly beautiful (unless you're into that sort of thing), but it was popular and mature. Python hadn't really become popular, nor did it really have a strong niche (at least as far as I could see). I went with Python because of its elegance, but since then, I've coded both p