python - 如何将一个python脚本的值返回到另一个?
问题描述
文件1.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 reply from processingfile,right?
处理文件.py
from file1 import ban
class sendfunction():
def reply():
reply = (ban.returnhello() + " replied")
return reply
我似乎无法得到任何结果,任何帮助将不胜感激。
解决方案
您需要在调用他之前创建object
类,如下所示ban
member function
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
方法作为static
方法。那么你不需要object
事先创建一个类来使用。
class ban():
@staticmethod # <---- this is how you make static method
def returnhello(): # Static methods don't require `self` as first arugment
x = "hello"
return x #gives reply a value of "hello replied" in processingfile
BTW:
好的编程习惯是,你总是以Capital
字母开头你的类名。
并且函数名和变量名应该是小写并带有下划线,所以returnhello()
应该是return_hello()
. 正如这里提到的。
推荐阅读
- mysql - MySQL SQL 多个
- schema - 如何在谷歌搜索结果中获得星级?那个代码是什么?
- xml - 如何使用 XSLT 转换 XML?
- c++ - 使用 callgrind 进行分析时 Sqlite 数据库为空
- javascript - 我的 js 代码给了我一个错误,说“属性未定义”,即使它存在于我的对象中
- javascript - 如何使用Vue从数据库中检索数据
- javascript - 为什么在 ajax 调用的 'then' 承诺中没有评估 'if' 条件?
- html - 量角器黄瓜 html 报告仅在第二次运行后生成?
- sorting - 在此示例中,如何按 Vegalite 中的特定字段对颜色堆栈进行排序?
- flutter - 如何将步骤的标题写为右边的单词和左边的另一个