extending methods ?

Arnd Baecker arnd.baecker at web.de
Wed Oct 1 09:25:26 EDT 2003


Many thanks for your response!

On Wed, 1 Oct 2003, Alex Martelli wrote:

> > More verbose: Assume the following situation that I have
> > a class class_A with several methods.
> > In each of these methods quite a few computations take place
> > and several variables are defined.
>
> Ah, no way, then -- local variables of a method (just like
> any other function) are totally internal to that method, and
> there is absolutely no reasonable way to access them from
> the outside.
>
> > However, with several variables and several methods
> > the code will not look nice.
>
> A matter of opinion.  I find it much nicer to know that a
> local variable is local, and an instance variable is not.

I absolutely agree. And that's why I wrote the (non-existing)
"appendto" in class_B,
  class class_B(class_A):
      appendto method1(self,x):
          print y                 # use the variable defined in class_A

The idea was that
everything from method1 in class_A is put into method1 in class_B
before the "print y", like a copy-and-paste operation.
Now, why could something like this be useful:
I have a situtation of a class with several methods
and now I would like to extend this class by
adding a few lines to each of the methods.
These further lines would make use of the variables defined
in the original methods.
An extension in this way would not require to
change anything in method1 of class_A
and any changes made in class_A carry over directly to
class_B.
(In a sense one could see this "copy" similar to a
"pre-processor" step  ...).

Instead of the above (which does not exist, and I'd guess never will ;-)
one could
  a) Invoke the superclass' method  in which all the
     relevant variables needed for the method in class_B are defined
     as self.<variable_name>. And then they can be
     accessed in class_B.
  b) Take a verbatim copy of class_A
     and just add the additional lines at the end
     of the corresponding methods.

     The drawback here is that any code changes
     in the common parts of the methods in class_A and class_B
     have to be applied in both parts.

So for b) it is the code duplication which I don't like
and for a) I don't like too much that one has to make
all the variables accessible self.<variable_name>=....
This adds a bunch of code which is only used when class_A
is subclassed.
(Of course  there is still option c) to write a small
python programm which takes class_A and does a "copy-and-paste"
operation at the corresponding places in class_B.)

Personally I tend to a) or c) ;-)
But maybe there is a better way of doing the above?

[ ... speed part snipped - many thanks I learned a lot!]

> > So is there a way to extend methods in the above sense
> > or is there a better (more pythonic ? ;-) approach to what I want?
>
> There is no pythonic way to treat local variables as non-local.

I think that this is not really what I want/meant,
as I thought more of the above internal "copy"
to avoid code duplication.

> (There MAY be horrible bytecode hacks exploiting hooks that are
> meant for debugging only -- but THOSE *WILL* slow you down
> SERIOUSLY...).

Oh no I am definitively not going along that route
- that's a different league ;-)

Many thanks,

Arnd





More information about the Python-list mailing list