Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python __subclasses__() not listing subclasses

Tags:

python

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

import.py
backends
      __init__.py
    --digger
          __init__.py
          base.py
          test.py
        --plugins
              plugina_plugin.py

From import.py i'm calling test.py. test.py in turn iterates over all the files in the plugins directory and loads all of them. test.py looks like this:

import os
import sys
import re

sys.path.append(os.path.join(os.path.abspath(os.path.dirname(os.path.abspath( __file__ )))))
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(os.path.abspath( __file__ ))), 'plugins'))

from base import BasePlugin

class TestImport:
    def __init__(self):
        print 'heeeeello'

        PLUGIN_DIRECTORY = os.path.join(os.path.abspath(os.path.dirname(os.path.abspath( __file__ ))), 'plugins')

        for filename in os.listdir (PLUGIN_DIRECTORY):
            # Ignore subfolders
            if os.path.isdir (os.path.join(PLUGIN_DIRECTORY, filename)):
                continue
            else:
                if re.match(r".*?_plugin\.py$", filename):
                    print ('Initialising plugin : ' + filename)
                    __import__(re.sub(r".py", r"", filename))

        print ('Plugin system initialized')
        print BasePlugin.__subclasses__()

The problem us that the __subclasses__() method doesn't show any derived classes. All plugins in the plugins directory derive from a base class in the base.py file.

base.py looks like this:

class BasePlugin(object):
    """
    Base
    """
    def __init__(self):
        pass

plugina_plugin.py looks like this:

from base import BasePlugin

class PluginA(BasePlugin):
    """
    Plugin A
    """
    def __init__(self):
        pass

Could anyone help me out with this? Whatm am i doing wrong? I've racked my brains over this but I cant seem to figure it out

Thanks.

like image 366
Mridang Agarwalla Avatar asked Jun 15 '10 19:06

Mridang Agarwalla


People also ask

How do I find my subclass in Python?

Python issubclass() is built-in function used to check if a class is a subclass of another class or not. This function returns True if the given class is the subclass of given class else it returns False . Return Type: True if object is subclass of a class, or any element of the tuple, otherwise False.

What is __ Init_subclass __ in Python?

In python __init_subclass__ can be used to implement class registries. In other words, this is keeping track of global or equivalent objects of the subclasses of a class that have been defined so that they can be easily accessed later.

How do you define a subclass in Python?

The process of creating a subclass of a class is called inheritance. All the attributes and methods of superclass are inherited by its subclass also. This means that an object of a subclass can access all the attributes and methods of the superclass.


1 Answers

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.

like image 62
Mridang Agarwalla Avatar answered Oct 05 '22 05:10

Mridang Agarwalla