pythonoptimizationdata-oriented-design

is it possible import variable from file to another and process then return the result to the first


First.py

x = 5
b = 6
from second import c
def print_():
   result = c
   print (ersult)
print_()

second.py

from First import x,b
class addition():
   def process(self):
       c = x + b
       return c

the variables in the first file (first)

the second file obtain the variables then do the function

then the first file obtain the result from second file and do the function

I wonder is it possible!!! or the right choice is the variable be separated in third file, or if the code isn't long, combine all in the same file


Solution

  • This won't work correctly because you have a circular import. First.py imports from second.py and visa versa. Rather than trying to import First.py into second, have addition.process take the necessary parameters to do your computation, import addition into First.py, and call addition.process their:

    second.py

    class addition():
       def process(self, x, b):
           c = x + b
           return c
    

    First.py

    x = 5
    b = 6
    from second import addition
    def print_():
       result = addition().process(x, b)
       print (result)
    print_()