Python Metaprogramming for Mad Scientists and Evil Geniuses
This talk from the 2012 PyCon covers the power and metaprogramming features of Python that cater to mad scientists and evil geniuses. This will also be of interest to others who just want to use of Python in a more power (hungry) way.
Users of Python are not limited to the usual model of a one-to-one correspondence between source code and live objects. Python allows you to synthesize functions, classes and modules without a direct correspondence to source code. You can mutate third-party objects, classes, modules and applications through monkey patching -- changing their behavior without altering their source code. You can even "chop-up" third-party objects to create new objects from the pieces. Find out how to unleash your inner Mad Scientist!
- Thesis: Python is an ideal language for both:
- Mad Scientists
- Evil Geniuses
- Mad Scientist versus Evil Genius
- Mad Scientist: creating new things because it's cool
- Evil Genius: practical applications
- Typical Mad Science Goals
- Create new living code objects from scraps without corresponding source code.
- Mutate third-party code to suite our purposes without modifying the third-party source code.
- Synthetics
- Synthetic Functions
- Synthetic Classes
- Synthetic Modules
- Applications of Synthetics
- Monkey Patching
- Monkey Patching Modules
- Monkey Patching Classes
- Monkey Patching Instances
- sitecustomize.py
- Dealing with Angry Villagers
- Limitations: When not to do this
- For the Evil Geniuses
Although most of the material is presented from the point of view of the Mad Scientist, it is equally useful to the Evil Genius.
Since the Python community prides itself on diversity, I should emphasize that the sane, the non-evil, and "do-gooders" are all welcome.