如何在不同 类(从同一父级继承)的实例之间传递相同的变量(可迭代)

How do I pass the same variable(iterable) between instances of different classes(that inherits from the same parent)

我想在不同 classes 的实例之间传递一个变量(可迭代)。我有一个类似于下面的结构。

每个 class 都有自己的模块(因此没有全局变量)并且需要在 python 3 和 2 中工作。

class O:
    pass


class A(O):
    pass


class B(O):

    def __init__(self, cache):
        self.cache = cache


class B1(B):

    def p(self):
        self.cache.add_to_cache("32", "something something")


class B2(B):

    def p(self):
        self.cache.get_from_cache("52",  "something else")

对于 B 及其子 class 我想创建一个缓存。此 classes(B, B1, B2) 的所有实例都使用相同的缓存。

为简单起见,假设缓存只是一个字典。

c = {}

a = A(c)
b1 = B() - needs c
b1.p()
b2 = C() - needs c
b2.p()

print(cache) 

当然上面的例子是错误的,因为每个实例的缓存都不同。

chache 应该是:

{
"32", "something something"
"52":  "something else"
}

要直接回答编程问题,请使用class variables

附带说明一下,最好使用某种“CacheService”并将其注入构造函数,而不是使用继承和 class 变量。
为此,请参阅


使用class变量的代码如下:

class O(object):
    pass

class B(O):
    __cache = {}  # use your cache class if you want, I am using dict just for show
    def __init__(self):
        pass

    def _get_from_cache(self, key):
        return self._cache.get(key, "default1")

    def _put_in_cache(self, key, value):
        self._cache[key] = value


class B1(B):
    def __init__(self):
        super(B1, self).__init__()

    def p(self):
        val1 = self._get_from_cache("a")
        print(val1)


class B2(B):
    def __init__(self):
        super(B2, self).__init__()

    def p(self):
        self._put_in_cache("a", 2)

if __name__ == "__main__":
    b1 = B1()
    b2 = B2()

    b2.p()
    b1.p()

输出:

2


注意 _get_from_cache_put_in_cache 是方法,但它们可以是 @staticmethod,因为它们只能访问 class 变量,并且它们的 self不是“真的”被使用过。 __cache 理论上可以由 children 直接访问,但是 _get_from_cache_put_in_cache 使 __cache 私有,并给它一个受保护的 API。

另一种方法是使用 CacheService 作为可注入的单例服务,我认为这是一种更好的做法。
,或继续阅读设计更好的解决方案。

class O(object):
    pass

class CacheService(object):
    __instances = {}

    @staticmethod
    def getinstance(owner_id):
        if owner_id not in CacheService.__instances:
            CacheService.__instances[owner_id] = CacheService(owner_id)
        return CacheService.__instances[owner_id]

    def __init__(self, owner_id):
        self._owner_id = owner_id
        self._owner_query = CacheService.__name__ + self._owner_id
        self._cache = {}

    def put_in_cache(self, key, value):
        self._cache[self._owner_query + str(key)] = value

    def get_from_cache(self, key):
        return self._cache.get(self._owner_query + str(key), "the_default")


class B(O):
    def __init__(self):
        self._cache = CacheService.getinstance(B.__name__)

class B1(B):
    def __init__(self):
        super(B1, self).__init__()

    def p(self):
        val1 = self._cache.get_from_cache("a")
        print(val1)


class B2(B):
    def __init__(self):
        super(B2, self).__init__()

    def p(self):
        self._cache.put_in_cache("a", 2)

if __name__ == "__main__":
    b1 = B1()
    b2 = B2()

    b2.p()
    b1.p()

输出:

2


这仍然使用 class 变量,但将其隐藏在您的“日常代码”中,并将其移至“基础结构级别”。
我认为这更清晰,因为现在您的 class 层次结构不应该处理它自己的全局变量。