sbose
sbose

Reputation: 1811

Python : Variable in a different module doesn't get changed

I have having issues with updating variables in one module from another module. I'll put it in the simplest way possible.

mymod.py

params= { "name" : "system_user" }

config.py

import myapp.mymod

myapp.mymod.params={}

However, the mymod.py remains unchanged.

Say if i try ,

myapp.mymod.params["name"]="oracle"

Even then the original .py file remains unchanged. Is there any way at all to achieve that?

Update: Right after posting the question, I realized how evil the feature could be, if present.

So I shall reframe my question and request suggestions for the same. I need to update a python module variable data so that another module is able to access the refreshed data.

One solution that comes to my mind is to add a static properties file which would used to re-fresh all the dictionary data in the python module. Thanks, Shoubhik

Upvotes: 0

Views: 76

Answers (1)

Ivo van der Wijk
Ivo van der Wijk

Reputation: 16785

You're assigning a new value to the "params" name, but any existing references to the original dictionary will remain unchanged.

myapp.mymod.params.clear() will probably update all other references since it doesn't reassign a new value to the "params" name but changes it in stead.

E.g.

>>> x = {"foo":"bar"}
>>> a = x
>>> x = {}
>>> print a
{'foo': 'bar'}

versus

>>> x = {"foo": "bar"}
>>> a = x
>>> x.clear()
>>> print a
{}

Upvotes: 3

Related Questions