0

I have file say test1.py and it has variable var1 = '' and in one of the methods in test1.py I'm updating value of var1 as

global var1 
var1 = 'new_value'

And I can see value updated by printing it from another method in test1.py. Now I have another file test2.py In which I'm calling var1 as var_new = test1.var1 But when I print it I still get ''. I think at the start of suite it's initializing all modules. But when I am calling method in test2.py it is getting value when it initialized? How can I get updated value? So var_new prints ''

If I use get_var() method then I get updated value. So I guess I have create the method always?

5
  • 1
    in test2.py try test1.var1="new_value" Commented Dec 22, 2015 at 0:41
  • are you saying assign new value from test2.py? If that's the case I can re-assign, but can't I use the value which I have already assigned or updated? Commented Dec 22, 2015 at 0:44
  • 1
    post your test1.py and test2.py so we can see..(I hope they are not lengthy) Commented Dec 22, 2015 at 0:48
  • well, going by your description it work fine to me, maybe the problem is where you modify var1?? you do it in a function in test1? better show us the code to see the problem Commented Dec 22, 2015 at 0:58
  • I edited, so is var_new suppose to have value '' or 'new_value'? Commented Dec 22, 2015 at 0:59

2 Answers 2

1

I think I finally understand the problem, by your description you have something like this

test1.py

var1=""

def foo():
    global var1
    var1="new_valor"

def bar():
    print(var1)

test2.py

import test1
var_new=test1.var1
print(var_new)

if that is the case, yes you get a empty string because you don't call foo to modify var1, so you have to call it first or modify from test2 because foo is not called at load time so var1 stay with the value it was first assigned.

Sign up to request clarification or add additional context in comments.

1 Comment

yes! thanks that's the right question. so even though if I call foo() before calling test2 it will print var1 as ''. I guess I have to use get method then. Thanks
0

By doing this:

var1 = test1.var1

you copy the content of the test1.var1 string into var1. But if later test1.var1 is modified, var1 is not.

>>> var1="test"
>>> var2=var1
>>> var2+= "a"
>>> var2
'testa'
>>> var1
'test'

So if you want to get the value of test1.var1, you should always access it threw test1.var1.

5 Comments

yes that's how I'm accessing but it prints '', I was hoping it will print 'new_value' since I modified it, won't it?
I should... I made the following test: test1.py ` global var1 var1='test' ` test2.py ` import test1 print test1.var1 ` $python test2.py test
before global var1, did you assign it as var1=''?
No, I assign it as 'test' after global var1. Then on test2 I get the correct value.
When I try to assign var1 before global, i get a warning "SyntaxWarning: name 'var1' is assigned to before global declaration", but it still works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.