0

Imagine this situation :

I have a file named settings.py that contains

from datetime import datetime
FOO = datetime.now()

Now, in another file, I do from django.conf import settings (the fact that I'm using Django in this specific situation is irrelevant)

In the file I did the import when I use settings.FOO, no matter how much time has passed I will always get the same datetime value, which corresponds to the time when the import happened.

I understand how and why it works that way. My question is : Is there a way to always have an updated value "stored" in settings.FOO without changing the way I access that value ? (as settings.FOO is used in multiple file across multiple subprojects, I aim to keep that syntax)

In other words, I there a way to make my imported variable transparently call a function ?

Ellynas
  • 105
  • 9
  • All inside variable are cached on first import, use the function to return dynamic data. Or you can reload module: https://stackoverflow.com/questions/437589/how-do-i-unload-reload-a-python-module – KiraLT Dec 04 '20 at 15:44
  • Variables never update themselves automatically. It doesn't matter whether it's imported or local. There's no way to make any variable act like a function. – Barmar Dec 04 '20 at 15:46
  • However, you can do it with object attributes using the @property declaration. So maybe you should declare a class in `settings.py`. Then you could use `settings_obj.FOO`. – Barmar Dec 04 '20 at 15:46

1 Answers1

0

As mentioned in the comments, there's almost never a good reason for doing it like this, and you would almost always rely on explicit callables for doing what you do, but by explicitly manipulating sys.modules, if all you care about is syntax, it is possible to do what you're trying to do.

settings.py:

from datetime import datetime
import sys

class _Bar:
   @property
   def FOO(self):
        return datetime.now()

sys.modules[__name__] = _Bar()

Usage example:

>>> import settings
>>> settings.FOO
datetime.datetime(2020, 12, 4, 17, 0, 5, 987423)
>>> settings.FOO
datetime.datetime(2020, 12, 4, 17, 0, 7, 285826)
fuglede
  • 17,388
  • 2
  • 54
  • 99
  • Since this answers my question accepted it. But I've noted your warning and will probably not use it. – Ellynas Dec 08 '20 at 15:09