complicated class question

Alex Martelli aleaxit at yahoo.com
Fri Nov 8 16:20:36 EST 2002


On Friday 08 November 2002 22:03, sismex01 at hebmex.com wrote:
   ...
> > So for example you'll have in mysyslib.py:
> >
> > import syslib as true_syslib
> > class a(true_syslib.a):
> >     def foo(self):
> >         print 'bye'
   ...
> 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?

Sure!  What OTHER definitions are there in that module?  How
would I know?  The OP didn't even mention the objects coming
from modules, much less anything about the usage of said
modules or any other contents thereof.  If module syslib has
other entries that must be preserved, then mysyslib must
"pass them straight through", e.g. with a "from syslib impot *".


> I thought that 'syslib.a' was only looked up when an instance
> of 'thirdparty.b' was created.  Opinion Alex?

When the code is, as I surmised (the OP didn't mention any
import nor other module use whatsoever):

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

then attribute named 'a' is looked up in global variable 'syslib'
of this module, and then called, each time method bar is called
on an instance of b.  If the module uses "from syslib import a"
instead, then the lookup is done when the "from" statement
executes.  In either case, it's possible to change an attribute
of this 'thirdparty' module after the class statement is
executed (and even after class b is instantiated) and before
method bar is called on b instances, and affect what ends up
in ainst -- but without knowing whether module 'thirdparty' uses
statement import or statement from, we can't know whether we
need to rebind thirdparty.syslib or else thirdparty.a.


Alex






More information about the Python-list mailing list