我有下面这样的课
class Paginator(object):
@cached_property
def count(self):
some_implementation
class CachingPaginator(Paginator):
def _get_count(self):
if self._count is None:
try:
key = "admin:{0}:count".format(hash(self.object_list.query.__str__()))
self._count = cache.get(key, -1)
if self._count == -1:
self._count = self.count # Here, I want to get count property in the super-class, this is giving me -1 which is wrong
cache.set(key, self._count, 3600)
except:
self._count = len(self.object_list)
count = property(_get_count)
如上面的注释所示,self._count = <expression>
应该在超类中获得count属性。如果是方法,我们可以这样叫它super(CachingPaginator,self).count()
AFAIK。我在报告中提到了许多问题,但都没有帮助我。有人能帮我吗。你知道吗
属性只是类属性。要获取父类的class属性,可以对父类使用直接查找(
Paginator.count
)或super()
调用。在本例中,如果对父类使用直接查找,则必须手动调用描述符协议,这有点冗长,因此使用super()
是最简单的解决方案:如果要直接查找父类,请替换:
与
相关问题 更多 >
编程相关推荐