complicated class question

Alex Martelli aleax at aleax.it
Fri Nov 8 14:42:02 EST 2002


Fred Clift wrote:
   ...
> class a:
>     def foo(self):
>         print "hi"
> 
> 
> class b:
>     def bar(self):
>         ainst = a()
>         ainst.foo()
> 
> 
> say that a is defined in a system library and that b is in a 3rd party
> library.

Then presumably you have, in thirdparty.py:

import syslib
class b:
    def bar(self):
        ainst = syslib.a()
        ainst.foo()

right?  So basically all you need to ensure before you import thirdparty
that ITS import of syslib will get YOUR tweaked version of module
syslib, and there you are.

So for example you'll have in mysyslib.py:

import syslib as true_syslib
class a(true_syslib.a):
    def foo(self):
        print 'bye'


> I'd like to get b to use a modified version a, without having to change
> the code of either
> 
> 
> my code would do something like:
> 
> binst = b()
> <insert your solution here>
> binst.bar()

You want to intervene too late, I think.  BEFORE you import thirdparty,
you'll have to install your tweaked mysyslib module as syslib.  So:

import sys
import mysyslib
sys.modules['syslib'] = mysyslib
import thirdparty
binst = thirdparty.b()
binst.bar()


Alex




More information about the Python-list mailing list