Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:如何将变量从一个Python脚本传递到另一个Python脚本_Python - Fatal编程技术网

Python:如何将变量从一个Python脚本传递到另一个Python脚本

Python:如何将变量从一个Python脚本传递到另一个Python脚本,python,Python,我知道以前有人问过这个问题,但我似乎找不到工作的答案。我试图将变量从一个脚本传递到另一个脚本 test.py def addstuff(word): things = word + " good." return things test2.py from test import addstuff addstuff("random") stuff = things + "morethings" 我也试着用 from test import * 事情没有按照test2中的定

我知道以前有人问过这个问题,但我似乎找不到工作的答案。我试图将变量从一个脚本传递到另一个脚本

test.py

def addstuff(word):
    things = word + " good."
    return things
test2.py

from test import addstuff

addstuff("random")

stuff = things + "morethings"
我也试着用

from test import *
事情没有按照test2中的定义显示,我如何解决这个问题?

addstuffrandom没有将addstuff函数的输出从test.py存储到任何变量中,它只是被丢弃

test2.py中的内容对程序没有任何意义,除非它没有分配给任何变量

以下是正确的方法:

from test import addstuff

things=addstuff("random")

stuff = things + "morethings"

我们正在分配AddStuffie random good的输出。添加到内容,然后向其中添加更多内容。

addstuffrandom只会丢弃结果。您没有将结果分配给命名变量,因此不会发生任何事情。它与导入过程无关,而是与如何使用函数things=addstuffrandom有关??这是否有帮助,在test2.pyA中应该是things=addstuffrandom远程相关建议:将代码视为由函数和类组成,不要再考虑脚本。您可以将Python类和函数放在一个文件或多个文件中,顺便说一句,我们称它们为模块,但这并没有多大区别。文件/脚本/模块之间没有通信-函数之间通过参数和返回值进行通信。是的,关于这一点的另一个评论是:test2.py不知道test.py中函数中命名或使用的变量是什么。它也不在乎。从test import*只导入test.py-stuff中本质上没有缩进的顶级内容的名称,它知道这些名称应该做什么/表示什么。