Reputation: 157
I have a file in my project - settings.py that contains all sorts of configurations such as x = {}
(a dict that later assigned with an updated value, which I will use here as an example), and these configurations are imported all over the code to be used by various business logic.
In main.py (which is where everything starts) I create a listener that listens to events and once triggered, it sets settings.x = {different dict}
.
However, if I go to another file where settings
is imported (before the change), and inspect the value of settings.x
, I don't see that the change took effect, meaning settings.x
over there holds the old value, and this confuses me.
Could it be because settings.py
was imported to the other file before the update took place? I thought it shouldn't be the case if I'm doing import settings
and then accessing settings.x
In any case I want it to be able to always take the last value of x, that is updated every once and then in main.py
.
Upvotes: 0
Views: 51
Reputation: 537
You could create a class out of your settings. Eg:
# Settings.py
class Settings:
x = {}
def getSettings(self):
return self.x
def setSettings(self, x):
self.x = x
myAppSettings = Settings()
.
#someFile.py
from settings import myAppSettings
def returnSettingsFromSomeFile():
return myAppSettings.getSettings()
.
from settings import myAppSettings
from somefile import returnSettingsFromSomeFile
def returnSettingsFromThisFile():
return myAppSettings.getSettings()
print(returnSettingsFromThisFile()) # {}
print(returnSettingsFromSomeFile()) # {}
myAppSettings.setSettings({'a': 1})
print(returnSettingsFromThisFile()) # {'a': 1}
print(returnSettingsFromSomeFile()) # {'a': 1}
Upvotes: 1