[英]How to use values from variables imported from another file python
so I know this has been asked in several forms before, but I cannot relate to any of those, either I have something different or I just don't understand them. 所以我知道以前已经以几种形式提出过这个问题,但是我不能与其中任何一种联系起来,要么我有所不同,要么就是我不理解它们。
The problem is I have script A and script B, and in script AI calculate and have all the variables I want to use in script B. 问题是我有脚本A和脚本B,并且在脚本AI中计算并拥有了要在脚本B中使用的所有变量。
Script A has various functions, let's say for now I just want to pass a simple number from a variable in script A to script B , let's call the variable value
. 脚本A具有各种功能,现在我只想将一个简单的数字从脚本A中的变量传递给脚本B,我们将其称为变量value
。
I used from script_A import value
. 我from script_A import value
。
Now, I have value
initialized in script_A with 0 right at the top to say so, but script_A processes value
, and gets a result clearly different from 0, but when I debug, I am getting in script_B value == 0
, and not value == calculated_value_that_should_be_there
. 现在,我已经在script_A中初始化了一个value
,在顶部的右上角这样说,但是script_A处理value
,并且得到的结果明显不同于0,但是当我调试时,我得到的是script_B value == 0
,而不是value == calculated_value_that_should_be_there
I did not know what to do so I tough about scope,so I put it in the return
of a function, I tried making variable value
a Global variable. 我不知道该怎么做,所以我对范围很苛刻,所以我把它放在函数的return
中,我试图使变量value
成为全局变量。 Nothing seems to work in the way that I am not passing the calculated 'value' but I am passing to script_B that 0 initialization. 我没有传递计算出的“值”,但我传递给script_B那个0初始化的方法似乎无济于事。
PS last thing I tried and what I saw from this topic is to import script_A as it was said with no namespaces. PS我尝试过的最后一件事,是从这个主题中看到的,是导入没有名称空间的script_A。 This has worked. 这已经奏效了。 When I write script_A.value it is calculated_value_that_should_be_there. 当我编写script_A.value时,它是calculated_value_that_should_be_there。 But, I do not know why anything else that I described did not work. 但是,我不知道为什么我描述的其他任何方法都不起作用。
script_A
from definitions import *
variable_1 = 0
variable_2 = 0
variable_3 = 0
variable_4 = 0
total = 0
respected = 0
time_diff = {}
seconds_all_writes = "write"
class Detect():
def __init__(self, data_manager, component_name, bus_name_list=None):
def __Function_A(self):
"""
global time_diff
global seconds_all_writes
process
script_B:
from script_A import respected
from script_A import total
import script_A
print aln_mon_detector.total
print aln_mon_detector.respected
I also want a dictionary 我也要字典
table_content.append(script_A.time_diff[file[script_A.seconds_all_writes]) table_content.append(script_A.time_diff [文件[script_A.seconds_all_writes])
I get 我懂了
KeyError: 'writes' KeyError:“写入”
this sounds a bit confusing without an example, but, in principle, what you're trying to do should work. 没有示例,这听起来有点令人困惑,但是,原则上,您尝试执行的操作应该可行。 Have a look at a minimum example below. 请看下面的最小示例。
ModuleA - defining the variable ModuleA-定义变量
# create the variable
someVariable = 1.
# apply modifications to the variable when called
def someFunc(var):
return var + 2
# ask for changes
someVariable = someFunc(someVariable)
ModuleB - using the variable ModuleB-使用变量
import moduleA
# retrieve variable
var = moduleA.someVariable
print(var) # returns 3
This probably has to do with immutability. 这可能与不变性有关。 Depends on what value
is. 取决于什么是value
。 If value
is a list (that is, a mutable object) and you append to it, the change should be visible. 如果value
是一个列表(即可变对象)并且您将其追加到列表中,则更改应该是可见的。 However, if you write 但是,如果您写
from module import x
x = 5
you are not changing the actual value, so other references to x
will still show the original object. 您没有更改实际值,因此对x
其他引用仍将显示原始对象。
If you have script A
like this: 如果您有这样的script A
:
# imports
value = 0
... # some calculations
Re-organize script A
as: 将script A
重新组织为:
# imports
def main():
value = 0
... # some calculations
return value
Now you can import script A
in script B
and run calculations
inside script B
: 现在,您可以导入script A
在script B
和运行calculations
内部script B
:
import script_A
value = script_A.main()
That is how you should organize code pieces in Python. 这就是您应该如何在Python中组织代码段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.