Python 每次调用时刷新配置变量值
模块具有如下所示的变量 config.pyPython 每次调用时刷新配置变量值,python,Python,模块具有如下所示的变量 config.py var_a = <some value that can change> 我希望var_a的值在每次调用后都会发生变化如果您希望每次都获得不同值的var_a,可以使用属性将其包装起来: config.py: import uuid class MyConfig: @property def var_a(self): return uuid.uuid4() my_conf = MyConfig() fr
var_a = <some value that can change>
我希望var_a的值在每次调用后都会发生变化如果您希望每次都获得不同值的
var_a
,可以使用属性将其包装起来:
config.py:
import uuid
class MyConfig:
@property
def var_a(self):
return uuid.uuid4()
my_conf = MyConfig()
from config import my_conf
print(my_conf.var_a)
print(my_conf.var_a)
模块副本:
import uuid
class MyConfig:
@property
def var_a(self):
return uuid.uuid4()
my_conf = MyConfig()
from config import my_conf
print(my_conf.var_a)
print(my_conf.var_a)
如果您希望每次获取具有不同值的变量a
,可以使用属性将其包装起来:
config.py:
import uuid
class MyConfig:
@property
def var_a(self):
return uuid.uuid4()
my_conf = MyConfig()
from config import my_conf
print(my_conf.var_a)
print(my_conf.var_a)
模块副本:
import uuid
class MyConfig:
@property
def var_a(self):
return uuid.uuid4()
my_conf = MyConfig()
from config import my_conf
print(my_conf.var_a)
print(my_conf.var_a)
您可以使用类方法:
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
import uuid
from config import var
var_a = var(uuid.uuid4)
print(var_a)
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()
import uuid
from config import var
var_a = var(uuid.uuid4)
print(var.get())
模块b.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
import uuid
from config import var
var_a = var(uuid.uuid4)
print(var_a)
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()
import uuid
from config import var
var_a = var(uuid.uuid4)
print(var.get())
这样,每次打印var\u a
时,它都会发生变化
但最简单的方法肯定不是存储uuid.uuid4()
,而是简单地调用它:
print(uuid.uuid4())
但是如果你做不到,我认为课堂上的选择是最好的
请注意,只有当您希望将值作为字符串返回时,才可以使用\uuuu repr\uuuu
,否则必须创建一个新方法并调用它,而不是打印
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
import uuid
from config import var
var_a = var(uuid.uuid4)
print(var_a)
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()
import uuid
from config import var
var_a = var(uuid.uuid4)
print(var.get())
模块b.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
import uuid
from config import var
var_a = var(uuid.uuid4)
print(var_a)
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()
import uuid
from config import var
var_a = var(uuid.uuid4)
print(var.get())
您可以使用类方法:
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
import uuid
from config import var
var_a = var(uuid.uuid4)
print(var_a)
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()
import uuid
from config import var
var_a = var(uuid.uuid4)
print(var.get())
模块b.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
import uuid
from config import var
var_a = var(uuid.uuid4)
print(var_a)
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()
import uuid
from config import var
var_a = var(uuid.uuid4)
print(var.get())
这样,每次打印var\u a
时,它都会发生变化
但最简单的方法肯定不是存储uuid.uuid4()
,而是简单地调用它:
print(uuid.uuid4())
但是如果你做不到,我认为课堂上的选择是最好的
请注意,只有当您希望将值作为字符串返回时,才可以使用\uuuu repr\uuuu
,否则必须创建一个新方法并调用它,而不是打印
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
import uuid
from config import var
var_a = var(uuid.uuid4)
print(var_a)
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()
import uuid
from config import var
var_a = var(uuid.uuid4)
print(var.get())
模块b.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
import uuid
from config import var
var_a = var(uuid.uuid4)
print(var_a)
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()
import uuid
from config import var
var_a = var(uuid.uuid4)
print(var.get())
如果您像这样导入模块:import config
然后像这样使用它:print(config.var_a)
它应该可以工作。
演示:
config.py
var\u a=0
二等兵
导入配置
def change_var_a():
config.var_a=1
main.py
导入配置
进口第二
assert config.var_a==0
第二,改变变量a()
assert config.var_a==1
如果您像这样导入模块:导入配置
,然后像这样使用:打印(config.var_a)
它应该可以工作。
演示:
config.py
var\u a=0
二等兵
导入配置
def change_var_a():
config.var_a=1
main.py
导入配置
进口第二
assert config.var_a==0
第二,改变变量a()
assert config.var_a==1
当您需要一个新值时,为什么不直接调用uuid.uuid4()
?这要明确得多。变量不是被调用的,而是被访问的。如果需要不同的值,则调用函数。你已经有一个了。用法:print(uuid.uuid4())
例如,我使用uuid传递值正在动态变化的消息。例如,如果我从第三方获得配置值,这可能会改变,这就是为什么需要此解决方案希望现在它可以并且容易理解这是否回答了您的问题?当需要新值时,为什么不直接调用uuid.uuid4()
?这要明确得多。变量不是被调用的,而是被访问的。如果需要不同的值,则调用函数。你已经有一个了。用法:print(uuid.uuid4())
例如,我使用uuid传递值正在动态变化的消息。例如,如果我从第三方获得配置值,这可能会改变,这就是为什么需要此解决方案希望现在它可以并且容易理解这是否回答了您的问题?我认为这是一个有问题的设计。您通常不希望表示函数更改值。同样令人困惑的是,对象的值实际上是一个可调用的返回值,而不是一个真正的值。现在更好了吗?在我看来,很多!唯一的问题是,callable
是一个内置函数,所以我会更改它!可执行文件更好吗?请注意,我也更喜欢第二个版本带有var\u a.get()
,但我也制作了带有\uu repr\uu()
的版本,因为在问题中,他使用了print(var\u a)
,我认为这是一个有问题的设计。您通常不希望表示函数更改值。同样令人困惑的是,对象的值实际上是一个可调用的返回值,而不是一个真正的值。现在更好了吗?在我看来,很多!唯一的问题是,callable
是一个内置函数,所以我会更改它!可执行文件更好吗?请注意,我也更喜欢使用var\u a.get()
的第二个版本,但我也使用了\uu repr\uu()
的版本,因为在问题中,他使用了print(var\u a)
我已经应用了这种类型的解决方案,但是如果配置中的变量太多,那么就太乱了,您将为所有这些变量创建一个属性variables@AnandTripathi您根本不需要属性
。您只需访问属性并覆盖\uuuuu getattribute\uuuuuu
您能举个例子吗@Tomerikoo你能举一个例子说明itI已经应用了这种类型的解决方案,但是当你在配置中有太多的变量,并且你将为所有这些变量创建一个属性时,它会变得非常混乱variables@AnandTripathi您根本不需要属性
。您只需访问属性并覆盖\uuuuu getattribute\uuuuuu
您能举个例子吗@你能举个例子吗