简体   繁体   中英

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

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

除了Christian Dean的答案,您还可以在这里看看: 从另一个传入args的python脚本中运行一个python脚本

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM