module inheritance? How to add a member to a class in a module

George Young gry at ll.mit.edu
Wed May 9 17:26:54 EDT 2001


[ SuSE Linux x86, python 2.1c2, PyGreSQL-3.2-pre010325 ]
I need to add one member function to a class that's inside a third-party
module.  The (single file) module has a bunch of classes and
module-level functions and
constants.  The module is pgdb.py from the PyGreSQL PostgreSql database
interface package.

It seems like I need to do something analogous to class derivation, but
at the module level.

Here's a sketch of the code; I'm trying to add the getnotify() member to
pgdbCnx:

----------------pgdb.py: the third party module I'd rather not modify
per se-----------
class pgdbCnx
        def __init__(self, cnx):
                self.__cnx = cnx
                self.__cache = pgdbTypeCache(cnx)
                src = self.__cnx.source()
                src.execute("BEGIN")
        def commit(self):
                src.execute("COMMIT")
#a few other classes and some constants and module-level functions...


---------------pgdb_enhanced.py---------
from pgdb import *              #just get everything

class __tmp(pgdbCnx):             #now I have pgdbCnx from the import, I
make new class from it
    def __init__(self):
        pgdbCnx.__init__(self)
    def getnotify(self):        # add my member func to this tmp class
        return self._pgdbCnx__cnx.getnotify()

class pgdbCnx(__tmp):             # shadow the imported class with this
new enhanced class
    def __init__(self):
        tmp.__init__(self)


Unfortunately, this doesn't work.  In my application I get:
Traceback (most recent call last):
  File "./er.py", line 433, in notified
    notify = db_conn.getnotify()
AttributeError: pgdbCnx instance has no attribute 'getnotify'

What is the proper pythonic way to do such a thing?


[Pygresql is available from ftp://ftp.druid.net/pub/distrib/PyGreSQL.tgz
]

-- 
Your mouse has moved.
Windows NT must be restarted for the change to take effect.
Reboot now?  [OK]



More information about the Python-list mailing list