question about overriding method

Allan Streib streib at
Thu Oct 30 00:36:51 CET 2003

Here is what I need to do:  The bsddb3 module
( defines a class called _DBWithCursor
which has a method called set_location.  The code for set_location
looks like:

    def set_location(self, key):
        return self.dbc.set(key)

I want to in effect override that method so that in the last line it
calls set_range(key) instead of set(key).

But, I never deal with that class directly; an instance of it is
returned by the function "btopen" which is defined in the
file of the bsddb3 module.  btopen is not a class method; it is just a
function defined in the bsddb3 namespace.

So when I write:

    import bsddb3
    mydb = bsddb3.btopen("mydbfile", "r")

how do I get mydb.set_location() to behave as I want?  I'm assuming
that there's a way to override this in my program; I don't really want
to change the bsddb3 code.

My solution is:

    import bsddb3

    def my_set_location(self, key):
      return self.dbc.set_range(key)


Which seems to do what I want.  Is there a better approach?


More information about the Python-list mailing list