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

Geoffrey Gerrietts 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.

Thanks,
--G.

---
Geoff Gerrietts <geoff at homegain.com>
Software Engineer, HomeGain.com
510-655-0800 x4320
 

> -----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 
> 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]
-- 
http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list