是否可以将变量从文件导入另一个文件然后处理 return 结果到第一个

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

不知道有没有可能!!! 或者正确的选择是将变量分开放在第三个文件中,或者如果代码不长,则将所有内容组合在同一个文件中

这将无法正常工作,因为您有一个循环导入。 First.pysecond.py 导入,反之亦然。与其尝试将 First.py 导入 second,不如让 addition.process 获取必要的参数来进行计算,将 addition 导入 First.py,然后调用 addition.process 它们的:

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_()

除了 Christian Dean 的回答,您还可以在这里查看:run a python script from another python script passing in args