A
Arnd Baecker
Hi,
the summary of my question is:
is there a way to append commands to a method inherited from
another class?
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.
Now I would like to extend this class to a class_B.
However, instead of overriding a method completely,
I would like to extend the method by basically adding
further commands at the end.
Eg., the code could look like:
class class_A:
def method1(self,x):
y=5*x+1 # create a variable
class class_B(class_A):
appendto method1(self,x):
print y # use variable defined in class_A
I.e. Effictively class_B should "look" like:
class class_B(class_A):
def method1(self,x):
y=5*x+1 # create a variable
print y # use the variable
Instead of this, one could in principle
store all variables defined in
method1 of class_A, eg. with self.y=y
and call in class_B the method1 of class_A.
However, with several variables and several methods
the code will not look nice.
(also efficiency is a bit of concern - presumably it shouldn't -
to me here as in my application the methods
are used to plot dots, circles, triangles etc.
so that speed is really important)
So is there a way to extend methods in the above sense
or is there a better (more pythonic ? ;-) approach to what I want?
Many thanks in advance.
Arnd
P.S.: Sorry if I messed up things on the OO side
(I am a real new-comer to this ...;-).
the summary of my question is:
is there a way to append commands to a method inherited from
another class?
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.
Now I would like to extend this class to a class_B.
However, instead of overriding a method completely,
I would like to extend the method by basically adding
further commands at the end.
Eg., the code could look like:
class class_A:
def method1(self,x):
y=5*x+1 # create a variable
class class_B(class_A):
appendto method1(self,x):
print y # use variable defined in class_A
I.e. Effictively class_B should "look" like:
class class_B(class_A):
def method1(self,x):
y=5*x+1 # create a variable
print y # use the variable
Instead of this, one could in principle
store all variables defined in
method1 of class_A, eg. with self.y=y
and call in class_B the method1 of class_A.
However, with several variables and several methods
the code will not look nice.
(also efficiency is a bit of concern - presumably it shouldn't -
to me here as in my application the methods
are used to plot dots, circles, triangles etc.
so that speed is really important)
So is there a way to extend methods in the above sense
or is there a better (more pythonic ? ;-) approach to what I want?
Many thanks in advance.
Arnd
P.S.: Sorry if I messed up things on the OO side
(I am a real new-comer to this ...;-).