complicated class question
sismex01 at hebmex.com
sismex01 at hebmex.com
Fri Nov 8 16:03:01 EST 2002
> From: Alex Martelli [mailto:aleax at aleax.it]
>
> 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()
>
But in this case, aren't you substituting the whole syslib
with your own module, instead of only the definition that
the OP wishes to change?
I thought that 'syslib.a' was only looked up when an instance
of 'thirdparty.b' was created. Opinion Alex?
-gustavo
More information about the Python-list
mailing list