For those of you who aren’t yet scared of Python, maybe these will help:

  • Recipe 299,777: subclass dict, assign self.__dict__ to point to self. I can’t believe this really works. For those of you wondering: __dict__ points to a dictionary that contains the fields of an object. By making a dict’s __dict__ point to itself, it exposes all of the dictionary’s mappings as fields of the dictionary itself. Ooh it’s all twisty.
  • Recipe 66,531: make a Singleton by having all instances share the same __dict__. Normally you make a Singleton class that constructs only one instance. Here instead you allow lots of instances, but make them share state. Brilliant.
  • Recipe 303,057: intercept name lookup in a specially marked method to provide Prolog-like functionality in Python. This uses Python 2.4 decorators to intercept method definition. It inspects the methods, finds out what names it refers to, and points those names at Prolog-like variables. I don’t quite follow everything going on in this recipe, but I like it!

I found these on Oliver Steele’s post about Python becoming Lisp. I studied Scheme, not Lisp, and I have to say that the above examples are not Schemish. Python has its own flavor of demonic insanity.

0 comments: