Hey guys,

Couldn't find the answer to this question in any old threads, probably because I don't know the terminology involved well enough.

I have a program which takes in data and writes it into a numpy array at the beginning of a pipeline. I use a class called Masterarray to take in the data and form it into an array.

Now I need to add the ability to take in the data and return a modified, but similar array. The new array, call it Darkarray, has all of the same dimensions and properties as the Masterarray, but the values are changed. To make this easy on myself, I set darkarray as an inheritance class so that my metacode looks like:

class Masterarray:
   def __init__(self, inlist, x_col, dark_col, ref_col):  
       ......
       ......
   def fcn1: 
       ......
   def fcn2:
       ......

class Dynamic_dark(Masterarray):
      	def __init__(self, inlist, x_col, dark_col, ref_col, sstart, ssend, lstart, lend):
        ......
        ......

My problem is that I have these functions, fcn1 fcn2 etc... that do very simple things, like return columns, rows and other simple operations. Since my inherited class, dynamic_dark is an array of equal dimensions, I thought I could just have these functions be inherited as well. But in my main program, when I call fcn1 or fcn2 even after instantiating the Dynamic_dark class, it returns values for these based on the original class. How do you inherit functions so to speak so that I can call them same function for either array?

Recommended Answers

All 5 Replies

So since posting this, a colleague pointed out that this is polymorphism. I'll try to read up on that and see if I can solve the problem. If you guys have anything to add, would still certainly be glad to hear it.

It is possible that you are setting class variables instead of setting values for instance in __init__. For me looks like you would not need two classes as you are not changing any functionality, but both your objects are of same type. Read up on Python philosophy of 'duck typing'. It is posible that what you would need could be also 'factory method'.

The bases of inheritance makes it clear .... that is if you inherit from a class you will have its methods into you new derived class. But and only but ....! you will not be able to genericaly call those methods in the parent class if you have the same methods in your class. The parent methods becomes like a virtual methods and only active when you have not over ruled them with your custom methods.

Hope i am clear. :)

Both of these suggestions were certainly helpful, but the information I could find on them seems pretty abstract. Sort of the kind of thing you'd study for a test in a programming class.

Let me put the questions this way and maybe the best solution will present itself:

If you have a code that is built around manipulating a matrix. Say you had 50 different functions that can operate on this matrix. You also have a very stable main program where everything is called in a nice sequence. Now, you decide you want to do the same thing on a new matrix. You can't just change the main program calls for the new matrix since it's already very elegant. Then all I need is one line that basically says (If choice A, make matrix A. If choice B, make matrix B), and then the rest of the code should operate on either matrix.

Anyway, I think the solution I'm going to make is this:

In my Masterarray class, I'm going to just put an if statement. If a keyword is specified, it will make the matrix of type A. If not, then type B. This seems like maybe the best solution, although I thought it was kind of a novice approach.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.