5

Given the following two .py files:

aclass.py

class A(object):
    pass

main.py

def importer(klass):
    """
    Used to import classes from there python qalname
    """
    import_ = lambda m, k: getattr(__import__(m, fromlist=k), k)
    klass = klass.split('.')
    module = '.'.join(klass[:-1])
    klass = klass[-1]
    return import_(module, klass)

from aclass import A

import_A = importer('aclass.A')
print isinstance(A(), import_A)  # Expected to be true 
print isinstance(import_A(), A)  # Expected to be true 

At this stage, everything works fine (my program prints True\nTrue) But if I modify the importer method to enforce a reload, ie:

this line:

    import_ = lambda m, k: getattr(__import__(m, fromlist=k), k)

is replaced by:

    import_ = lambda m, k: getattr(reload(__import__(m, fromlist=k)), k)

my programs returns

False
False

And I do not understand this behavior.

ohe
  • 3,461
  • 3
  • 26
  • 50

1 Answers1

9

Reloading a module means re-executing its content, in this case class A(object): pass. So it creates another different class. It's the same behavior as:

class A(object):
    pass
a = A()
class A(object):          # a different class
    pass
print isinstance(a, A)    # False

This should be enough to explain why a bare reload() is usually a bad idea. I'm sure others could point to frameworks that implement more sophisticated reloading procedures, e.g. patching the old class to be considered equal to the new one.

Armin Rigo
  • 12,048
  • 37
  • 48
  • `others could point to frameworks`, I would love that others points me such implementations! – ohe Apr 08 '13 at 11:41
  • 1
    For more information: http://stackoverflow.com/questions/437589/how-do-i-unload-reload-a-python-module – Armin Rigo Apr 08 '13 at 22:04