我正在开发一个 Flask 扩展,为 Flask 添加 CouchDB 支持。为了使它更容易,我已经子类化couchdb.mapping.Document
so the store
and load
方法可以使用当前线程本地数据库。现在,我的代码如下所示:
class Document(mapping.Document):
# rest of the methods omitted for brevity
@classmethod
def load(cls, id, db=None):
return mapping.Document.load(cls, db or g.couch, id)
为了简洁起见,我省略了一些内容,但这是重要的部分。但是,由于类方法的工作方式,当我尝试调用此方法时,我收到错误消息
File "flaskext/couchdb.py", line 187, in load
return mapping.Document.load(cls, db or g.couch, id)
TypeError: load() takes exactly 3 arguments (4 given)
我测试过将呼叫替换为mapping.Document.load.im_func(cls, db or g.couch, id)
,并且它有效,但我对访问内部并不是特别高兴im_
属性(即使它们已被记录)。有谁有更优雅的方法来处理这个问题?
我认为你实际上需要使用super
这里。无论如何,这是调用超类方法的更简洁的方法:
class A(object):
@classmethod
def load(cls):
return cls
class B(A):
@classmethod
def load(cls):
# return A.load() would simply do "A.load()" and thus return a A
return super(B, cls).load() # super figures out how to do it right ;-)
print B.load()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)