python - 通过将属性合并/更新到模块的命名空间来加载一次配置

标签 python python-3.x namespaces python-module

在 Python 中使用以下配置单例类:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function
from builtins import *
import sys

class Config(object):

    __instance = None

    def __new__(cls):
        if cls.__instance is None:
            cls.__instance = super(Config, cls).__new__(cls)
            cls.__instance.__initialized = False
        return cls.__instance

    def __init__(self):
        print('INIT')
        if self.__initialized: 
            return
        self.__initialized = True

        self._defaults = {"foo": True, "bar": False}

        for k, v in self._defaults.items():
            setattr(self, k, v)

    def reload(self):
        print(self)
        if not self._defaults:
            return
        for k, v in self._defaults.items():
            setattr(self, k, v)

我可以编写一个测试来验证属性是否正确设置和更新:

import config; config = config.Config()

print(config)
print(config.foo)
assert(config.foo is True)
config.foo = False
print(config.foo)
assert(config.foo is False)

config.reload()
print(config.foo)
assert(config.foo is True)

输出:

INIT
<config.Config object at 0x10bbf73d0>
True
False
<config.Config object at 0x10bbf73d0>
True

这工作正常,但我的强制症无法处理我必须导入配置并执行其主类。

我尝试添加sys.modules[__name__] = Config()config.py 的底部,这样我就可以做 import config ,但它对 reload() 犹豫不决与:

TypeError: 'NoneType' object is not callable

奇怪的是,我只希望它会提示模块 namespace 上的值没有更新,但它似乎事先就中断了 - 只是试图执行 reload() .

我是不是运气不好?或者有什么神奇的方法可以让我同时使用 reload()和一行字import config

最佳答案

当您重新加载时它不起作用的原因是,在分配给sys.modules[__name__]之后,该模块及其内置函数会被垃圾收集并替换为None。我假设您知道这一点,因为您 frombuiltins import *。但是,导入的内置函数也将被垃圾收集。

要解决此问题,您可以在 reload 中导入内置函数,这样您使用的版本就不会被垃圾收集。

旁注:这未经测试,因为我无法重现您的问题 here ,但我相信这种情况会发生,因为该网站在导入方面可能会很不稳定。

关于python - 通过将属性合并/更新到模块的命名空间来加载一次配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49039628/

相关文章:

function - 命名空间::function cannot be used as a function

python - 我如何使用opencv实现居中剪切图像

python - 从字符串日期中提取一年中的某一天和儒略日

python - 如何在 django shell 中重新加载模块?

python-3.x - 将值替换为数据帧的最早日期时间记录

python - 输出错误: Find the longest path in a matrix with given constraints

python - 在 Tensorflow 2.0 中通过前辈增加张量的每个元素

python - 如何在 python 或 matplotlib 中为非常小的值绘制条形图?

c# - WPF 命名空间的奇怪名称

c++ - 了解导入在 C++ 中的工作原理