为基类的所有子类创建一个可哈希的id

时间:2013-03-17 16:59:35

标签: python oop inheritance

对于缓存/持久性原因,我希望获得一个基类的任意大型子类树(它永远不会自己创建)的唯一可扩展地址。以下是目前的情况:

OBJECT_CACHE = dict()

class Base(object):
    def __new__(cls, *args, **kwargs):
        # calculate class_addr here?
        obj = OBJECT_CACHE.get(class_addr)
        if obj is None:
            obj = super(Base, cls).__new__(cls, *args, **kwargs)
            OBJECT_CACHE[class_addr] = obj
        return obj

但是我不确定获得这样一个id的最佳方法是什么。我的概念是它看起来像下面这样:

Base:          # no id
    F          # id = 'f'
    A:         # id = 'a'
        E      # id = 'a.e'
        B:     # id = 'a.b'
            C  # id = 'a.b.c'
            D  # id = 'a.b.d'

我考虑尝试使用属性和super(),但理想情况下,子类只包含一行my_id = 'e'。提前谢谢!

1 个答案:

答案 0 :(得分:2)

您要找的是cls.mro()

OBJECT_CACHE = dict()
class Base(object):
    def __new__(cls, *args, **kwargs):
        class_addr = cls.name()
        obj = OBJECT_CACHE.get(class_addr)
        if obj is None:
            obj = super(Base, cls).__new__(cls, *args, **kwargs)
            OBJECT_CACHE[class_addr] = obj
        return obj
    @classmethod
    def name(cls):
        names = [k.__name__.lower() for k in cls.mro() 
                if k != Base and issubclass(k, Base)]
        return '.'.join(names[::-1])


class F(Base): pass
class A(Base): pass
class E(A): pass
class B(A): pass
class C(B): pass
f = F()
a = A()
e = E()
b = B()
c = C()
print(OBJECT_CACHE.keys())

产量

['a', 'a.e', 'a.b.c', 'a.b', 'f']