module inheritance? How to add a member to a class in a modu le
geoff at homegain.com
Wed May 9 23:54:43 CEST 2001
I can't tell you the RIGHT way to do this.
The easy way, though, is to just do it.
In your code, do:
>>> def getnotify(self):
>>> return self._pgdbCnx__cnx.getnotify()
>>> import pgdb
>>> pgdb.pgdbCnx.getnotify = getnotify
That should work just like magic.
Geoff Gerrietts <geoff at homegain.com>
Software Engineer, HomeGain.com
> -----Original Message-----
> From: George Young [mailto:gry at ll.mit.edu]
> Sent: Wednesday, May 09, 2001 2:27 PM
> To: python-list at python.org
> Subject: module inheritance? How to add a member to a class
> in a module
> [ SuSE Linux x86, python 2.1c2, PyGreSQL-3.2-pre010325 ]
> I need to add one member function to a class that's inside a
> 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
> ----------------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()
> def commit(self):
> #a few other classes and some constants and module-level functions...
> 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):
> 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):
> 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
Your mouse has moved.
Windows NT must be restarted for the change to take effect.
Reboot now? [OK]
More information about the Python-list