IsPython really O-O?

Ken Seehof kseehof at neuralintegrator.com
Wed Nov 14 20:46:05 EST 2001


> "Andrew Dalke" <dalke at dalkescientific.com> wrote .
> > Me:
> > >>I wanted to add a new method, so I did
> > >>
> > >>class Spam:
> > >>  def double(self):
> > >>    self.x = self.x * 2
> >
> > Daniel Klein:
> > >Just curious to know how it is that you expected this to work
> >
> > Because I didn't think that 'class Spam' always created a new
> > class.  I thought it would reuse a definition of one already exists.
> > I believe this is similar to what Smalltalk does.
> >
> > > why you were 'annoyed'?
> >
> > Because it didn't fit my model of how the language worked, so
> > was more effort to learn.
> >
> > >I can't
> > >see what the problem is with the double() method.
> >
> > The problem was I wanted to add a method to an existing class.
> > The method definition was fine.
> >
> ISTR someone suggested some time ago that we might use
>
> def Spam.double(self):
>     self.x *= 2
>
> to add or redefine methods, but I don't remember why this was thought to
be
> a bad idea. I suppose in all such cases it's bad that there's no way to
> reach out to existing instances and modify them. SmallTalk's introspection
> system has it over Python in that respect, but then (like me :) it's more
> mature.
>
> memory's-not-what-it-was-ly y'rs  - steve
> --
> http://www.holdenweb.com/

If the syntax you describe were implemented, it would presumable modify the
class directly, and therefore would affect all existing instances (the
instances
all refer to the same class object, so there is no reason to need to reach
out
to the existing instances).  There isn't any semantic reason against it, in
fact
here's a recipe describing how to add or replace a method in an existing
class
using the 'new' module:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81982

So python seems to be just as flexible as smalltalk is this respect, but you
have to get under the hood a bit more.

I've used this trick on a few occaisions; usually for debugging, but also to
work around design problems in existing python packages.

- Ken






More information about the Python-list mailing list