Tao of the Machine

Programming, Python, my projects, card games, books, music, Zoids, bettas, manga, cool stuff, and whatever comes to mind.

The Self language

After ABC, I have the honor to present to you... the Self language!

For an introduction, that turns conventional ideas about OO upside down, see this tutorial. In short, Self doesn't use classes; rather, it uses "objects". You stick methods and other objects in it to make it a prototype; then, instead of instantiating it, you copy the prototype. Inheritance works different as well, etc. But I'm sure I've only brushed the surface here...

Anyway, I wondered if we could emulate this in Python. Here's a first stab at it:

class SelfMethod:
    def __init__(self, obj, f):
        self.obj = obj
        self.f = f
    def __call__(self, *args, **kwargs):
        return self.f(self.obj, *args, **kwargs)

class SelfObject:
    def copy(self):
        # overly simple version for testing purposes.
        so = self.__class__()
        for key, value in self.__dict__.items():
            setattr(so, key, value)
        return so
    def __setattr__(self, name, value):
        if callable(value):
            if isinstance(value, SelfMethod):
                sm = SelfMethod(self, value.f)
                self.__dict__[name] = sm
            else:
                self.__dict__[name] = SelfMethod(self, value)
        else:
            self.__dict__[name] = value
The SelfMethod class is a wrapper. We need it, because we're going to write functions and pretend they're methods, which only works if we can pass them an actual object to work on. SelfMethod holds this object and the function.
# the canonical SelfObject
SelfObject = SelfObject()

# how to make a Point object?
Point = SelfObject.copy()
Point.x = Point.y = 0
Note that copy() is the Self equivalent of instantation. Now, let's inject some methods in this Point object:
# some methods for the Point class
def move(self, dx, dy):
    self.x = self.x + dx
    self.y = self.y + dy
def repr(self):
    return "Point(%s,%s)" % (self.x, self.y)

# inject it
Point.move = move
Point.repr = repr
__setattr__ conveniently wraps these in SelfMethod instances. (Kind of kludgy; maybe there's a better way...)
# now create our own Point
mypoint = Point.copy()  # this invokes __setattr__ again!!
print mypoint.repr()
mypoint.move(4,5)
print mypoint.repr()

# create another one
print Point.repr(), "should still be (0,0)!"
yourpoint = Point.copy()
print yourpoint.repr(), mypoint.repr()
This example does not take the special inheritance into account (or any of the Self features that I haven't discovered yet ;-). Most likely I'll have to reinvent several wheels, inheritance lookup etc. Also, the current copy() method is very shallow, which will cause side effects for mutable objects (change a list in one object, find out that it has also changed in the other...).

What's the value of this? Is it useful? Who knows. Just like aikido has Ki training, I like to think that exercises like these strenghten one's Pythonic Ki. :-)

Posted by Hans Nowak on 2002-11-21 22:40:57   {link}
Categories: Python, programming

The ABC language

This site isn't new to me, but I thought I'd mention it because it offers an interesting overview of Python's predecessor. Many of the ideas in ABC made it into Python, although sometimes in a different form. Some highlights:
  • Indentation is used to define block structure.
  • An example page demonstrating the basic types shows longints, "multiplying" of strings (although the operator was ^^ rather than **), slices (with a weird syntax using @ and |), lists (in curly braces!) and dictionaries.
  • ABC had named function parameters, a bit Smalltalk-ish: HOW TO ADD element TO relation FOR thing, etc.
  • Interestingly, ABC had a switch-like statement (called select), which obviously did not make it into Python.
  • Neither did the SOME, EACH and NO tests. These are easily written as Python functions though.

Posted by Hans Nowak on 2002-11-21 12:41:27   {link}
Categories: Python, programming

HP2

OK, I've seen the movie. Some points: (everything that follows is IMHO, of course)
  • It's not as good as the first one, but that is probably because TCoS is generally considered to be weaker than the first one. Also, we've already seen many of the eye candy (Hogwarts, spells, Quidditch fx, etc) in the first part, so that is not quite so fascinating anymore.
  • I don't agree with the criticism that it contains many parts that aren't relevant to the plot. The scene at home doesn't take too long, and it's a reminder of the environment Harry is in when he's not at Hogwarts. Meeting the Weasleys is crucial in the overall story (although maybe not for this particular book). The flying car is not just there for the special effects either, it returns later in the movie.
  • Hogwarts does seem claustrophobic. It just seems too small, the halls too narrow, the common rooms too cramped.
  • My main "gripe" is that the movie, although 2h45m, is just too short to deal with everything in the book. There are not-so-minor characters and events that are skipped for the movie, simply because there isn't enough time, obviously. (Who would watch a 6-hour movie? I would, I guess, if you could bring real food into the movie theater...) This is true for many books-as-movies, of course (heck, LotR is much worse in this regard).
Next month: The Two Towers (maybe)...

Posted by Hans Nowak on 2002-11-19 18:18:47   {link}
Categories: general

<grunt>

  • Lots of work.
  • Thinking about a good domain name.
  • Got cats.
  • No work on personal projects. :-(
  • It's cold.
  • Gotta go see that Harry Potter movie.
  • Want aikido classes.
Nuff said.

Posted by Hans Nowak on 2002-11-18 11:42:20   {link}
Categories: general

--
Generated by Firedrop2.