Tao of the MachineProgramming, Python, my projects, card games, books, music, Zoids, bettas, manga, cool stuff, and whatever comes to mind. |
Front page Last 10 archives 2002-08-31 2002-09-14 2002-09-21 2002-09-28 2002-10-05 2002-10-12 2002-10-19 2002-10-26 2002-11-02 2002-11-09 Category archives bettas books CCGs Charm esoterica Firedrop games general ideas internet Kaa linkstuffs Lisp manga music Mygale Nederland nostalgia programming Python SGI switch Wax Zoids :: Search All posts ...by category ...by date ![]() :: About me :: Oasis Digital :: Resume :: GeoURL :: RSS :: Atom ![]() ![]() |
The Self languageAfter 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] = valueThe 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 = 0Note 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. :-) The ABC languageThis 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:
HP2OK, I've seen the movie. Some points: (everything that follows is IMHO, of course)
<grunt>
|