1

A module is having variables like below

config.py

var_a = <some value that can change>

If the above variable is called in some another module then it will give you same value if running in some kind of server. I am using flask server.

module_b.py

from config import var_a
print(var_a)

I want the value of var_a will change after every call to it

Anand Tripathi
  • 14,556
  • 1
  • 47
  • 52
  • 4
    Why don't you just simply call `uuid.uuid4()` when you need a new value? That is much more explicit. – Håken Lid Feb 18 '21 at 11:29
  • 1
    Variables are not called, they are accessed. If you want a different value, then call a function. You already have one of those. Use: `print(uuid.uuid4())` – quamrana Feb 18 '21 at 11:29
  • 1
    For example, I have used uuid to convey the message that the value is dynamically changing. For example, if I am getting config values from third party and that may change that's why needed this solution – Anand Tripathi Feb 18 '21 at 11:33
  • Hope now its okay and easy to understand – Anand Tripathi Feb 18 '21 at 11:34
  • Does this answer your question? [Using global variables between files?](https://stackoverflow.com/questions/13034496/using-global-variables-between-files) – Tomerikoo Feb 18 '21 at 11:38
  • Have you tried importing it like: `import config` and then using it like: `print(config.var_a)`? I think it should work. – Roy Cohen Feb 18 '21 at 11:55
  • It will give you static value will not change on every call – Anand Tripathi Feb 18 '21 at 11:57

3 Answers3

2

If you want to get the var_a with different value each time, you can wrap it with property:

config.py:

import uuid

class MyConfig:
    @property
    def var_a(self):
        return uuid.uuid4()

my_conf = MyConfig()

module_b.py:

from config import my_conf

print(my_conf.var_a)
print(my_conf.var_a)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Menglong Li
  • 2,177
  • 14
  • 19
1

you can use class methods:

config.py

class var:
    def __init__(self, executable):
        self.executable = executable
     
    def __repr__(self):
        # here change/refresh val, maybe recalling it
        return str(self.executable())

    def __eq__(self, other):
        #here you compare the new value with another one, it's called when you do val_a == other
        if self.executable() == other:
            return True
        else:
            return False
          

module_b.py

import uuid
from config import var
var_a = var(uuid.uuid4)
print(var_a)

In this way every time you print your var_a it will change.

But surely the easiest way would be not to store uuid.uuid4() and simply calling it:

print(uuid.uuid4())

But if you can't do it I think that the class alternative is the best one.

note that using __repr__ will work only if you want the value to be retured as string, else you have to create a new method and call it instead of print

config.py

class var:
    def __init__(self, executable):
        self.executable = executable
     
    def __eq__(self, other):
        #here you compare the new value with another one, it's called when you do val_a == other
        if self.executable() == other:
            return True
        else:
            return False

    def get(self):
        # here change/refresh val, maybe recalling it
        return self.executable()

module_b.py

import uuid
from config import var
var_a = var(uuid.uuid4)
print(var.get())
Leonardo Scotti
  • 1,069
  • 8
  • 21
  • 1
    I would say that this is a problematic design. You generally don't want the representation function to change the values. And it's also confusing that the `value` of the object is actually a callable returning a value, and not really a *value* – Tomerikoo Feb 18 '21 at 11:42
  • is it better now? – Leonardo Scotti Feb 18 '21 at 11:48
  • In my opinion, a lot! Only issue is that `callable` is a built-in function so I would change that name – Tomerikoo Feb 18 '21 at 11:49
  • note that i prefer the second version with `var_a.get()` too, but i made also the one with `__repr__()` because in the question he used `print(var_a)` – Leonardo Scotti Feb 18 '21 at 11:59
  • @LeonardoScotti just another requirement do you know how can I override the type of var class to str!! – Anand Tripathi Feb 18 '21 at 12:41
  • do you want var class to be a str? – Leonardo Scotti Feb 18 '21 at 13:01
  • let say the output is 111-111 and you do var_a == '111-111' then it evaluates to false cause, __repr__ doesnt return the type of the value it is always the type of class. – Anand Tripathi Feb 18 '21 at 14:28
  • I want if I am retrning the value from __repr__ as str then it should be comapared to str and actually work like str if __repr__ is returning list then it should be list – Anand Tripathi Feb 18 '21 at 14:29
  • you could overwrite another builtin class method, the `__eq__` one, i edited the answer, take a look at it – Leonardo Scotti Feb 18 '21 at 14:33
0

If you import the module like so: import config and then use it like so: print(config.var_a) it should work.
Demonstration:
config.py

var_a = 0

second.py

import config
def change_var_a():
    config.var_a = 1

main.py

import config
import second

assert config.var_a == 0
second.change_var_a()
assert config.var_a == 1
Roy Cohen
  • 1,540
  • 1
  • 5
  • 22