Reputation: 15
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 A I calculate and have all the variables I want to use in script 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
.
I used 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
.
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. 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.
P.S last thing I tried and what I saw from this topic is to import script_A as it was said with no namespaces. This has worked. When I write script_A.value it is 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])
I get
KeyError: 'writes'
Upvotes: 0
Views: 1577
Reputation:
If you have script A
like this:
# imports
value = 0
... # some calculations
Re-organize script A
as:
# imports
def main():
value = 0
... # some calculations
return value
Now you can import script A
in script B
and run calculations
inside script B
:
import script_A
value = script_A.main()
That is how you should organize code pieces in Python.
Upvotes: 0
Reputation: 447
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
# 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
import moduleA
# retrieve variable
var = moduleA.someVariable
print(var) # returns 3
Upvotes: 1
Reputation: 29099
This probably has to do with immutability. Depends on what value
is. If value
is a list (that is, a mutable object) and you append to it, the change should be visible. 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.
Upvotes: 0