Tao of the Machine

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

By the way...

Does anybody know how I can make the editor window in Kaa stretch *vertically*? I tweaked the Tkinter code a bit here and there, and now it stretches horizontally when you resize the window, but not vertically yet.

Posted by Hans Nowak on 2002-11-23 00:47:00   {link}
Categories: Kaa

The Selfish programmer

I've written some new code that more or less emulates Self-style classes. Here's how to use it:
  1. To create a vanilla object, instantiate SelfObject.
  2. Stick attributes and methods in it:
    obj.x = 42
    obj.deposit = myfunction
    
  3. Attributes can be retrieved, and functions can be called, in the usual way:
    bankaccount.deposit(100)
    print bankaccount.balance
    
  4. Use Self-like inheritance by sticking any SelfObject in a slot, and designate that it's a parent by using the _p suffix:
    bankaccount.clonable_p = selfish.clonable
    # use the 'clonable' object from the selfish.py module
    
    (In Self, parents have an asterisk as suffix, but that obviously isn't a valid Python identifier, so I chose _p instead.)

    clonable has a copy() method. Suddenly, bankaccount can use this method too:

    johns_account = bankaccount.copy()
    freds_account = bankaccount.copy()
    
    Such is the power of Selfish inheritance. :-)
The code I posted yesterday should still work with this new framework, yet the internals are different, and inheritance works now. The selfish.py module (and tests) is available for download. For a limited time; after a while I'll remove the zip files with experimental and development code. When I have my New Site, it will probably reappear there. As usual, the code is not for serious purposes, may and probably will contain bugs, etc. Hey kids, this is just a toy thingy.

Self is kinda interesting... you can start out with an "empty" object, then tack functionality on it, or inherit from other objects (not: classes) on the fly. Very flexible. Of course, Python supports this too, in a way... but it's different; for example, adding methods to a class is easy, but you cannot easily stick a method in an instance and expect it to work, not without some trickery.

Posted by Hans Nowak on 2002-11-23 00:10:41   {link}
Categories: Python, programming

--
Generated by Firedrop2.