Search code examples
pythonclassinstance-variablespython-module

python - how to share variables between two different instances


I have seen many posts that shows how to access constant value, by inheritance. that's not my case.
I would like to create two instances from two different modules, and be able to share information/variables between them.
two issues:
1. the classes "dont know" each other, because they are on different files, "import"ing them as modules does not help much in this case
2. how can "print_class" instance can access & change values of variable in "cal_class" instance ?

Please note, I am looking for a way to access value from one instance to another. (in my case, there is allot of information (excel tables) to pass between instances, passing parameters through the "main" function is not practical)

Here is my simplify problem:

cal_class_file.py

class cal_class:
    def add(self,first,second)
        self.result= first + second

print_class_file.py

class print_class:
    def print_sum(self):
       result_from_cal_class = ?? <=-# How to get the value from local_calculate.result?
       print(result_from_cal_class)

main.py

import cal_class_file
import print_class_file

def main():
    local_calculate = cal_class_file.cal_class() # create instance
    local_print = print_class_file.print_class(); # create instance that reads values from "local_calculate" instance

    local_calculate.add(5,6) # calculate addition
    local_print.print_sum() # output: 11

    local_calculate.add(4,5) # update values in "local_calculate" instance
    local_print.print_sum() # output: 9

How can I get the current (Latest) value for "result_from_cal_class" ?


Solution

  • Pass the value in. You have it right there in your client code:

    class print_class:
        def print_sum(self, result_from_cal_class):
           print(result_from_cal_class)
    
    def main():
        local_calculate = cal_class_file.cal_class() # create instance
        local_print = print_class_file.print_class(); # create instance that reads values from "local_calculate" instance
    
        local_calculate.add(5, 6) # calculate addition
        local_print.print_sum(local_calculate.result) # output: 11
    
        local_calculate.add(4, 5) # update values in "local_calculate" instance
        local_print.print_sum(local_calculate.result) # output: 9
    

    Output:

    11
    9
    

    However, if you mean that your client does this:

    def main():
        # setup elided ...
    
        # actually call to add is done in another function
        local_calculate.add(5, 6) # calculate addition
    
        # actually call to print_sum is done somewhere else:
        local_print.print_sum() # output: 11
    

    You could define print_class like this:

    class print_class:
        def __init__(self, calculator):
            self.calculator = calculator
        def print_sum(self):
           print(self.calculator.result)
    

    Then initialise it like this:

    def main():
        local_calculate = cal_class_file.cal_class() # create instance
        local_print = print_class_file.print_class(local_calculate); # create instance that reads values from "local_calculate" instance
    

    Be careful as this is a little like having a global variable: calculate does something, leaving a result, print picks up the last result.

    If you have an unexpected call to calculate between the one you want and the print, you will get unexpected results.