I Am Not Charles


Advogato #1

Originally posted on Advogato on March 22, 2004:

Fun with Python

A. M. Kuchling, in rec.arts.int-fiction, just showed me a really neat Python trick which he attributed to Michael Hudson. But first, some background:

I use a special-purpose language called Inform to write interactive fiction. It’s sort of a hybrid between standard class-based and prototype-based OO languages – there’s a shorthand to create a single object and add properties and methods to it, but if you want to create a bunch of identical objects you still need to create a class for them. It’s very handy for world modelling. Most of the specialized IF languages use the same scheme.

I’ve been thinking for a while about using a standard language – Python or Ruby, by preference – since every IF language has annoyances and weirdnesses. This involves writing a standard library for world-modelling, which is a big job. There are already a few for Python, but they’re really cumbersome compared to the Inform standard library, because creating a new object with a few specialized behaviours always involves both a new class and a new object.

Behold – prototyped Python (direct from amk’s post)! Now we get to see how well the whitespace survives it’s trip through HTML and back:

Wow. The answer to that is “not at all”. The <pre> tag appears to do precisely nothing.

Instead, the code is on my Wiki page at work. To summarize: it lets you say “class SpecialRoom(Room)” and get both a class SpecialRoom and an instance (also conveniently named SpecialRoom) automatically.

This still has a few weirdnesses. Unlike a true prototype-based language, there are still classes and objects, so you can’t directly say “issubclass(SpecialRoom, Room)” – you need to “issubclass(SpecialRoom.__class__, Room.__class__)”. I’ll need to experiment and read up some more to find out exactly how the class and the instance differ, and decide how to make it more seamless (or even if it’s worth it). This also can’t handle dynamically creating objects, which can be handled by adding a clone() method:

 def clone(self): class anon(self): pass return anon 

Except I’m not sure exactly where to put this. I tried initializing it in Prototype.__new__, which works fine but apparently isn’t correct – that’s what Prototype.__init__ is for, except it’s not actually getting called in the above code.

Still, this is lots of fun! Massive new project, here I come…

Commentary: The massive new project never materialized. I updated the wiki link to go directly to the relevant page, where you can see why – it’s a big disorganized mess of “here’s one way to do it” and “here’s another way to do it”. It was an interesting subject, though, and Python’s gotten a bunch of new features since then (decorators came out just after the original post) so I should really get back to that. It would be nice to pull it all together into a complete solution.

Advertisements

2 Responses to 'Advogato #1'

Subscribe to comments with RSS or TrackBack to 'Advogato #1'.


  1. […] noticed this while I was following links from my Advogato entries. Apart from the Python stuff it’s the only thing there that would fit in on a technical blog. Sooner or later […]

  2. Mark J Musante said,

    Inform isn’t just a neat way to do data initialization, although that’s certainly a major part of it — especially from an IF game developer’s standpoint, but there’s all the underlying glue code, and of course the in-game parser.

    Don’t get me wrong: it does sound like an interesting project. I look forward to seeing it in action.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: