Okay...
I have searched and searched looking for an answer that directly answers my question, but have had no success. My problem is pretty straight forward and I honestly thought there would have been a more direct answer out there. Please keep in mind I am still relatively new to the language, and am still learning.
So I will use fileA
and fileB
as my two files, and x
as my example variable. Variable x
is contained in fileB
. How do I go about importing variable x
into fileA
, then change x
to another value via raw_input
, and then have the variable x
update in fileB
with the new value?
I'm not sure if this is even possible, but I would sure like to think so. I am using python 2.7.11, thank you in advanced.
A common way to do this is to put values into a dictionary, list or even in a class (or a mutable instance of one). Then you can import the container object and mutate it to change the value you care about. Very concise and precise answer.
Can I import Python function from another file? To use the functions written in one file inside another file include the import line, from filename import function_name . Note that although the file name must contain a . py extension, .
In JavaScript, variables can be accessed from another file using the <script> tags or the import or export statement. The script tag is mainly used when we want to access variable of a JavaScript file in an HTML file. This works well for client-side scripting as well as for server-side scripting.
If the "variable" you're referring to is an mutable value, what you're asking for will just work.
fileB:
my_variable = ["a list with a string in it"]
fileA:
from fileB import my_variable # import the value
my_variable.append("and another string")
After fileA
has been loaded fileB.my_variable
will have two values in it.
But, that only works for mutable values. If the variable is immutable, the code in fileA
can't change it in place, and so you'll have issues. There's no way to directly fix that, but there are many ways to work around the issue and still get at what you want.
The easiest will simply be to use import fileB
instead of from fileB import my_variable
. This lets you anything in fileB
's namespace, just by using a name like fileB.whatever
. You can rebind things in the namespace to your heart's content:
fileB:
my_variable = 1 # something immutable this time
fileA:
import fileB
fileB.my_variable = 2 # change the value in fileB's namespace
That is probably the simplest approach.
Another solution would be to put the immutable variable inside a mutable container, and then modify the container, rather than the variable. For instance, if the string "a list with a string in it"
was the value we wanted to change my the first example, we could simply assign a new value to my_variable[0]
(rather than appending).
A common way to do this is to put values into a dictionary, list or even in a class (or a mutable instance of one). Then you can import the container object and mutate it to change the value you care about.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With