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 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 newFunctions, classes, and modules are just objects in memory.
module = new.module(...)
sys.modules['name'] = module
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)The room was packed. This was a very popular talk.
obj.spam = new.instancemethod(new_spam, obj, MyClass)
This is how to "fix" executables:
- Create a special version of sitecustomize.py containing your fix.
- Create a wrapper script for the executable that sets PYTHONPATH to contain the dir containing sitecustomize.py.
He monkeypatched __import__ so that code gets executed whenever someone tries to import something! Wow! I've never seen that trick before!
sitecustomize.py 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.
More evil genius tools:
- Speed things up with cython and ctypes
Using the ast module is another approach.
"new" is deprecated (in Python 3, I think). It's been replaced by the "types" module.