loose methods : Smalltalk asPython

  • Thread starter Jan Theodore Galkowski
  • Start date
J

Jan Theodore Galkowski

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
 
S

Steven D'Aprano

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.

You can't modify the built-in classes. I'm not sure that it is a good idea
to allow built-ins to be modified. When I see an int, I like the fact that
I know what the int can do, and I don't have to worry about whether it has
been modified by some piece of code elsewhere.

But custom classes can be modified (the risk of some other code modifying
it is the price you pay for being able to customise the class in the first
place):
.... pass
........ return bool(self % 2)
....
True


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(.).

Why not just say isinstance(x, Foo)?

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?

Yes you can. Did you bother to try it?
.... pass
....'<__main__.Shrubbery object at 0xb7d06aec>spamspamspamspamspam'
 
B

bearophileHUGS

Steven D'Aprano:
You can't modify the built-in classes. I'm not sure that it is a good idea
to allow built-ins to be modified. When I see an int, I like the fact that
I know what the int can do, and I don't have to worry about whether it has
been modified by some piece of code elsewhere.

I think it can be useful, there are many situations where you may need
to change the behavior of built-in dicts, etc, but:
- You need to keep code sorted and tidy to avoid problems. (This is
true for most of Python code too today. For example ObjectPascal gives
you less freedom to shoot yourself in the foot.)
- Allowing the built-in objects to be dynamically modified like that
may slow down the virtual machine some, if you don't design it quite
carefully. Psyco can help here too.

Bye,
bearophile
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top