Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我们可以设置可以在会话中修改的模块级属性吗_Python_Python 3.x - Fatal编程技术网

Python 我们可以设置可以在会话中修改的模块级属性吗

Python 我们可以设置可以在会话中修改的模块级属性吗,python,python-3.x,Python,Python 3.x,我正在尝试向配置文件添加一个模块级属性,并希望执行类似的操作 # repo/__init__.py file pass # repo/config.py file VERIFY_CERTS = True 然后,在其他子模块中使用此变量 # repo/example.py from repo.config import VERIFY_CERTS def do_something(): if VERIFY_CERTS: do something.. else:

我正在尝试向配置文件添加一个模块级属性,并希望执行类似的操作

# repo/__init__.py file
pass

# repo/config.py file
VERIFY_CERTS = True
然后,在其他子模块中使用此变量

# repo/example.py
from repo.config import VERIFY_CERTS
def do_something():
    if VERIFY_CERTS:
        do something..
    else:
        do something else
现在,当我在另一个脚本中使用此回购模块时,我希望能够做到:

from repo.config import VERIFY_CERTS
VERIFY_CERTS = False
from repo.example import do_something
do_something()
有可能这样做吗

编辑:它绝对不是的副本,因为它讨论了一些关于可变和不可变数据类型的内容,而这一部分是关于具有一个可以在会话中记住的模块级属性。 修改了变量名以澄清我为什么要这样做。

问题 据我所知,您想更改
VERIFY\u CERTS
do\u something()
使用的值

首先,您的示例可以简化如下:

示例.py

VERIFY_CERTS = True
def do_something():
    print(VERIFY_CERTS)
from example import do_something
VERIFY_CERTS = False
do_something()
test.py

VERIFY_CERTS = True
def do_something():
    print(VERIFY_CERTS)
from example import do_something
VERIFY_CERTS = False
do_something()
运行
test.py
将打印
True


解决方案 在
test.py
中,只需导入整个模块
示例
(这是导入的最佳实践),然后设置
示例。验证证书

import example
example.VERIFY_CERTS = False
example.do_something()
运行此操作将打印
False

问题 据我所知,您想更改
VERIFY\u CERTS
do\u something()
使用的值

首先,您的示例可以简化如下:

示例.py

VERIFY_CERTS = True
def do_something():
    print(VERIFY_CERTS)
from example import do_something
VERIFY_CERTS = False
do_something()
test.py

VERIFY_CERTS = True
def do_something():
    print(VERIFY_CERTS)
from example import do_something
VERIFY_CERTS = False
do_something()
运行
test.py
将打印
True


解决方案 在
test.py
中,只需导入整个模块
示例
(这是导入的最佳实践),然后设置
示例。验证证书

import example
example.VERIFY_CERTS = False
example.do_something()

运行此命令将打印
False

我知道您想做什么,但我看不到它的应用程序。比如,为什么您需要导入X,为什么您需要一个不在文件顶部的导入?可能是@wjandrea重命名变量的重复,以使其更清晰我知道您想要做什么,但我看不到它的应用程序。比如,为什么您需要导入X,为什么您需要一个不在文件顶部的导入?可能是@wjandrea重命名变量的重复,以使其更清晰感谢您的响应。但是,这只是一个大项目的一小部分,所以我不能简化它来删除配置。我是通过使用不同的方法来解决这个问题的。@manji369哦,我假设
repo
是一个第三方模块,您无法控制,您只是在编写一个使用它的脚本。谢谢您的回复。但是,这只是一个大项目的一小部分,所以我不能简化它来删除配置。我是通过使用不同的方法使用类变量来解决这个问题的。@manji369哦,我假设
repo
是一个第三方模块,您无法控制,您只是在编写一个使用它的脚本。