Disabling Cache?

Alex Martelli aleaxit at yahoo.com
Fri Aug 17 11:19:24 EDT 2001


"Angel Asencio" <asencio at mitre.org> wrote in message
news:3B7D2983.77C3CBE3 at mitre.org...
> Greetings:
>   I know that I can do "reload" to load a module all over again.  But I do
not want to trace all the modules
> to determine which ones are dependent and reload them.

You don't have to reload any other module, as long as you don't use
the statement 'from x import y' (or otherwise stash attributes of
module x away somewhere), in which case the only real solution
is to stop doing so -=- use 'import x' and always refer to x's attributes
as x.pleep, x.kloop, etc, explicitly, and you'll have no problem.


>   Is there a way to disable the caching?  I am tired of shutting down IDLE
to reset stuff.

There is no 'caching' that reload(x) doesn't bypass, except what
you choose to use yourself.  E.g., if you choose to  code in your
module z:
    from x import y
or
    import x
    kleep = x.y
then there's no way to disable this 'caching' that you're doing
yourself.  Just use x.y explicitly everywhere you need it, and then
a reload(x) will have all your modules using the newly reloaded x.

In some cases you may find you've bound attributes of (the old)
x, and those don't get re-bound.  One case is where you've used
a default-attribute, e.g. still in module z:

def plik(a, b=x.y):
    print a,b

As the default value is evaluated once when the def statement
executes, this is, alas, a 'caching' although that may not be
obvious.  You may want to use a slightly more cumbersome idiom
to work around that, e.g.:

def plik(a, b=None):
    if b is None: b=x.y
    print a,b

Now there's no "caching" effect.

One really troublesome case is where you want to inherit
from classes that are attributes of module x.  E.g.:

class Zz(x.Yy):
    pass

*NOW* Zz.__bases__ is (x.Yy,) -- it's "cached", and it's hard
indeed to avoid this.  But, unfortunately, just as for any other
case of user-coded 'caching', there's no way to 'disable' it
(forbid you from inheriting from x's classes...?).  Is this the
specific problem you're having?


Alex






More information about the Python-list mailing list