Richard Jones' Log: Using "dict" as a base class

Wed, 19 May 2004

Here's a relatively trivial example of using dict as a base class to create a dict that returns True or False on item accesses as if has_key() was called:

class TruthDict(dict):
    def __getitem__(self, key):
        return self.has_key(key)

I use this in Zope code(*) to make a simple var that I can shove various strings into and later test for the existence of an arbitrary string. Why not use has_key()? Because with this code I can perform tests using simpler ZPT path expressions instead of having to use python expressions.

*: of course, being a Zope object used in templating, there's an additional class var __allow_access_to_unprotected_subobjects__ = 1 (yeah, old school Zope) that I've omitted from the example code to make it more readable.

... just thought I'd let everyone know I was still alive. Just very busy with work and Roundup :)

Comment by Alan Green on Fri, 21 May 2004

mmmmm.... new Roundup. Looking forward to it.

Comment by Richard on Fri, 21 May 2004

At the moment, the codebase is having translations forcibly inserted into various places. That'll most likely be the Big Feature of the next release. I'm kinda pooped after the last release :)