Main contents

Archive for September 2007

Language Abstractions

September 1st, 2007

It’s been nice working at a company where everyone is a computational polyglot – you can debate the merits of LISP or the shortcomings of Smalltalk over coffee. Of course there are downsides – simply to get the build system to work requires getting Perl, C, Bash and Python to work together harmoniously.

However, working in a semantically rich language, like Python, or talking to people who know Lisp, which is probably the most versatile language in existence, really does through into sharp relief how semantically impoverished C is. One problem is that people frequently don’t make use of language abstractions, even where they exist. But it’s very nice to be able to introduce them, and benefit from the resulting simplification of the code base.

I discovered the new python ‘with’ statement, which in the absence of deterministic destructors is a pretty nice way of handling resource management. Writing lock-safe code is now as simple as this:


with Guard(lock):
    threadUnsafeOperation()

where Guard is as simple as

class Guard:
    def __init__(self,lock):
        self.lock=lock
    def __enter__(self):
        self.lock.acquire()
    def __exit__(self,exceptionType,exceptionValue,traceback):
        self.lock.release()

I’ve really been missing the constructor/destructor pairs from C++ in other languages, and this seems to fit the bill pretty nicely, especially where the pattern you’re looking for is ‘ScopeGuard’

Posted in Programming, python | No Comments »