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

问题描述

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

每个类都有自己的模块(所以没有全局变量)并且需要在 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 及其子类,我想创建一个缓存。该类的所有实例(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”并将其注入构造函数,而不是使用继承和类变量。
为此,请参阅 my other answer


使用类变量的代码如下:

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,因为它们只访问类变量,并且它们的 self 不是“真正的”一直被使用。 __cache 理论上可以由孩子直接访问,但 _get_from_cache_put_in_cache__cache 设为私有,并为其提供受保护的 API。

,

另一种方法是使用 CacheService 作为可注入的单例服务,我认为这是更好的做法。
Read this first for a code/syntax solution to your direct question,或继续阅读具有更好设计的解决方案。

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,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


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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...