I have 2 files let say them a.py
and b.py
, b.py
has a global variable let say test
(data type is a string), and I’m importing test
in a.py
. Now, during runtime, a function in a.py
calls a function of b.py
, and that function changed the value of test
in b.py
but this change doesn’t show in a.py
because the test is of string datatype and string is immutable.
I tried using the list, and it works, but I’m not feeling very good about using a list with one element in it. So, is there any mutable data type(similar to string) that I can use for this purpose?.
Code when the test
is string.
b.py
test = "hello"
def changeTest():
global test
test = "hii"
a.py
from b import test,changeTest
def callFunctionInB():
changeTest()
print(test)
callFunctionInB()
print(test)
output:
hello
hello
Code when the test is list.
b.py
test = ["hello"]
def changeTest():
test[0] = "hii"
a.py
from b import test,changeTest
def callFunctionInB():
changeTest()
print(test)
callFunctionInB()
print(test)
output:
['hello']
['hii']