loose methods : Smalltalk asPython

Jan Theodore Galkowski jtgalkowski at alum.mit.edu
Tue Dec 26 22:49:30 EST 2006


Hi.

One of the things I'd like to do with Python is find a way to
consistently implement Smalltalk's "loose methods".  This is a
capability whereby additional  methods can be added dynamically to
existing classes.

In some respects, it's possible with Python. While "object" cannot be
touched, it's possible to define

   class MutableObject( object ) :  pass ;

and derive subclasses from MutableObject instead of object.  Thereafter,
for instance, if devising a class

   class Foo( MutableObject ) :   isFoo = True ;   ...

you can also put in its module

    MutableObject.isFoo = False ;

So, at least for the inheritance hierarchy descending from
MutableObject, for an arbitrary object instance x,

    x.isFoo

will return whether it's a Foo or not, effectively although not
transparently extending the builtin type(.).

Like, then, what of other classes which descend from object and not
MutableObject?  You'd love to be able to set methods and attributes
within them.  Can you?  These provide on-the-spot extensions of their
capability without having to subclass them (not such a big deal) or get
everyone to use the new subclass (a very big deal).

Comments?  Suggestions?

Thanks,

  -- Jan



More information about the Python-list mailing list