如何将值从一个python脚本返回到另一个python脚本?
file1.py如何将值从一个python脚本返回到另一个python脚本?,python,python-3.x,python-import,cross-reference,Python,Python 3.x,Python Import,Cross Reference,file1.py from processing file import sendfunction class ban(): def returnhello(): x = "hello" return x #gives reply a value of "hello replied" in processingfile print(sendfunction.reply()) #this should fetch the value of repl
from processing file import sendfunction
class ban():
def returnhello():
x = "hello"
return x #gives reply a value of "hello replied" in processingfile
print(sendfunction.reply()) #this should fetch the value of reply from processingfile,right?
processingfile.py
from file1 import ban
class sendfunction():
def reply():
reply = (ban.returnhello() + " replied")
return reply
我似乎真的无法得到任何结果,任何帮助都将不胜感激。在调用他的
成员函数之前,您需要创建类ban
的对象
from file1 import ban
class sendfunction():
def reply(self): # Member methods must have `self` as first argument
b = ban() # <------- here creation of object
reply = (b.returnhello() + " replied")
return reply
顺便说一句:
好的编程实践是,你总是以大写字母开始你的类名。
函数名和变量名应该是带下划线的小写,因此returnhello()
应该是return\u hello()
。如前所述。假设我们有两个文件A.py和B.py
A.py
B.py
在执行B.py时,您会得到以下输出:
└> python B.py
saying hi in A
The value of a is 3 in B
处理文件
导入时不应该有空格。哦,很抱歉,我会做出修改,谢谢@RahulChawla
a = 3
print('saying hi in A')
from A import a
print('The value of a is %s in B' % str(a))
└> python B.py
saying hi in A
The value of a is 3 in B