Question

I am working on a Flask extension that adds CouchDB support to Flask. To make it easier, I have subclassed couchdb.mapping.Document so the store and load methods can use the current thread-local database. Right now, my code looks like this:

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)

I left out some for brevity, but that's the important part. However, due to the way classmethod works, when I try to call this method, I receive the error message

  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)

I tested replacing the call with mapping.Document.load.im_func(cls, db or g.couch, id), and it works, but I'm not particularly happy about accessing the internal im_ attributes (even though they are documented). Does anyone have a more elegant way to handle this?

Was it helpful?

Solution

I think you actually need to use super here. That's the neater way to call superclass methods anyway:

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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top