Python __subclasses__() not listing subclasses

前端 未结 2 1523
离开以前
离开以前 2021-01-05 09:11

I cant seem to list all derived classes using the __subclasses__() method. Here\'s my directory layout:

import.py
backends
      __init__.py
            


        
相关标签:
2条回答
  • 2021-01-05 09:14

    There were no other base.py files. I'm on a WinXP (SP2) with Python 2.6. I added another class to my test.py file called PluginB which used BasePlugin as the base class. When i did

        print PluginA.__mro__
        print PluginB.__mro__
    

    I got:

    (<class 'plugina_plugin.PluginA'>, <class 'base.BasePlugin'>, <type 'object'>)
    (<class 'backends.digger.test.PluginB'>, <class 'backends.digger.base.BasePlugin'>, <type 'object'>)
    

    As you can see, they're both using the same base plugin but the qualified names are different. This was because in plugina_plugin.py I was importing BasePlugin like this:

    from base import BasePlugin
    

    Instead of:

    from backends.digger.base import BasePlugin
    

    Fixing this fixed it.

    0 讨论(0)
  • 2021-01-05 09:21

    Perhaps the plugins are finding another base.py hiding somewhere (the plugin directory for example).
    Run with python -v to see if two different base.py are getting imported

    You can also look at PluginA.__mro__ and confirm that the BasePlugin in there is the right one

    0 讨论(0)
提交回复
热议问题