autoreload and package causing TypeError: super(type, obj): obj must be an instance or subtype of type

后端 未结 2 1974
遥遥无期
遥遥无期 2021-02-07 06:34

I have python code spanning several files which I packaged up for convenience, ending up with the following 3 files under my_package directory:

__init__.py
         


        
2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-07 07:24

    I'm not a user of IPython, so I can't say exactly what's happening, but I guess that this is a symptom of using from file2 import * in your __init__.py file.

    When you're creating an instance of your Derived class using package.Derived, you're not getting the most recent version of the class, but the old version that was current when the package was first loaded and the from file2 import * statement was run. When you modified the module code and IPython reloaded it, that changed package.file2.Derived, but not package.Derived.

    However, the old version of the class still has a reference to its module's namespace, and when it tries to look itself up by name in the super call, it finds the newer version of the class instead. This is why you get an error, as the two Derived classes are not the same.

    You would probably avoid this issue if you accessed package.file2.Derived directly. That will always get you to the current version of the class which shouldn't have any issues with super calls. Note that if there are still instances of the class that were created before you modified the module, you may still have issues (but that's probably not very surprising).

提交回复
热议问题