Search code examples
python-3.xinheritancepython-class

Is it possible for an instance of a class within a different class to inherit its variables


I have 2 classes, one does calculations calc and the other sets up and runs setuprun the calculation.

I have multiple instances of the calc class in setuprun to run multiple calculations in one go for each tab. method from here

At the moment they are separate classes but I'd like to inherit variables from setuprun to calc, specifically excelbook and cases, to remove some duplicated code. I have tried various combinations of parent, child and super with no luck.

I guess my question is; is it possible for an instance of a class within a different class to inherit its variables - as i write this, my feeling is not...

Any pointers would be much appreciated. Thanks.

code below:

class calc():

    def __init__(self, excelbook, tab, cases):
        self.excelbook = excelbook
        self.cases = cases
        self.tab = tab

    def run(self):
        print(self.excelbook, self.cases, self.tab)


class setuprun():

    def __init__(self, excelbook, cases):
        self.excelbook = excelbook
        self.gettabs()
        runs = [calc(excelbook, t, cases) for t in self.tabs]
        for run in range(len(runs)):
            runs[run].run()
        self.check = runs

    def gettabs(self):
        self.tabs = list(self.excelbook)

can run with:

a = setuprun('xlsx', 100)

Solution

  • As i write this, my feeling is not... Do not underestimate Python ;)

    There is something strange about your code that I don't understand: you are taking the length of the string 'xlsx' to instantiate the calc classes. It just prints

    xlsx 100 x
    xlsx 100 l
    xlsx 100 s
    xlsx 100 x
    

    That's fine with me though, but probably not correct in your final application.

    After you instantiate the class setuprun, it instantiates 4x a class calc() each time with the same values for excelbook and cases. These two variables are instance variables of the class setuprun.

    What you can do is turn them into class variables. After that, you can reference them directly, hence, you do not need to instantiate class calc with the common variables.

    Because it is good practice to use capitalized names for classes (to distinguish them from instance variables) I do so in the adapted code below:

    class Calc():
    
        def __init__(self, tab):
            self.excelbook = Setuprun.excelbook
            self.cases = Setuprun.cases
            self.tab = tab
    
        def run(self):
            print(self.excelbook, self.cases, self.tab)
    
    class Setuprun():
    
        def __init__(self, excelbook, cases):
            Setuprun.excelbook = excelbook
            Setuprun.cases = cases
            self.gettabs()
            runs = [Calc(t) for t in self.tabs]
            for run in range(len(runs)):
                runs[run].run()
            self.check = runs
    
        def gettabs(self):
            self.tabs = list(self.excelbook)
            
    a = Setuprun('xlsx', 100)
    

    I'm not quite sure if this is what you were looking for, but I do hope it answers your question.