2

Let's say I've a file a.py and a has a variable var.

In file b.py, I imported, a.py and set a.var = "hello".

Then I imported a.py in c.py and tried to access a.var but I get None.

How to reflect the change in data? It is obvious that two different instances are getting called, but how do I change it?

Edit:

What I am trying to do here is create a config file which constitutes of this:

CONFIG = {
    'client_id': 'XX',
    'client_secret': 'XX',
    'redirect_uri': 'XX'
    }

auth_token = ""

auth_url = ""

access_point = ""

Now, let's say I use config.py in the script a.py and set config.access_point = "web"

So, if I import config.py in another file, I want the change to reflect and not return "".

Edit:

Text file seems like an easy way out. I can also use ConfigParser module. But isn't it a bit too much if reading form a file needs to be done in every server request?

Hick
  • 35,524
  • 46
  • 151
  • 243
  • 5
    importing a file does not open it in "write" mode, it just evaluates the code and adds it to your namespace. When you import it again in another module, you are evaluating the code in the file again. Please explain what is the actual problem you are trying to solve (with sample code) to give you a better response. – Burhan Khalid Oct 29 '12 at 09:43
  • Already covered: http://stackoverflow.com/questions/592931/why-doesnt-python-have-static-variables – ChristopherC Oct 29 '12 at 09:58
  • This should actually work in the way you described. Have you made sure that the code is executed in the order as you describe? – silvado Oct 29 '12 at 10:24
  • No I don't think it should work. It would work if I used config.access_point across the entire a.py script but not in c.py. In c.py, it will be reinstatiated again and will always return a "". – Hick Oct 29 '12 at 10:28
  • Do you want to save the changed configuration? Consider some other file format, eg. look into [the ConfigParser module](http://docs.python.org/2/library/configparser.html). – Janne Karila Oct 29 '12 at 10:38
  • Maybe using a config parser module in a server for every request would make it every slow. – Hick Oct 29 '12 at 11:43

3 Answers3

2

As a preliminary, a second import statement, even from another module, does not re-execute code in the source file for the imported module if it has been loaded previously. Instead, importing an already existing module just gives you access to the namespace of that module.

Thus, if you dynamically change variables in your module a, even from code in another module, other modules should in fact see the changed variables.

Consider this test example:

testa.py:

print "Importing module a"
var = ""

testb.py:

import testa
testa.var = "test"

testc.py:

import testa
print testa.var

Now in the python interpreter (or the main script, if you prefer):

>>> import testb
Importing module a
>>> import testc
test
>>>

So you see that the change in var made when importing b is actually seen in c.

I would suspect that your problem lies in whether and in what order your code is actually executed.

silvado
  • 17,202
  • 2
  • 30
  • 46
  • I don't think this is what the OP is trying to do. Although his question is a little ambiguous, so I could be wrong. – Matt Oct 29 '12 at 11:24
1

In file a.py define a class:

class A:
    class_var = False
    def __init__(self):
        self.object_var = False

Then in b.py import this and instantiate an object of class A:

from a import A
a = A()

Now you can access the attributes of the instance of a.

a.object_var = True

And you can access variables for the class A:

A.class_var = True

If you now check for:

a.class_var
-> True
a.object_var
-> True
another_instance = A()
another_instance.object_var
->False
another_instance.class_var
->True
Schuh
  • 1,045
  • 5
  • 9
0

well , i'd use an text file to set values there,

a.py :
with open("values.txt") as f:
  var = f.read()#you can set certain bytes for read 

so whenever you import it , it will initialise new value to the var according to the values in the text file , and whenever you want to change the value of var just change the value of the text file

Hamoudaq
  • 1,490
  • 4
  • 23
  • 42