I recently came across a Python class factory implementation that fit a problem I am working on really well. The only difference is that I wanted have the base and sub classes in different packages.
When I try to do this, however, I run into a problem whenever I try to load the base class.
from subclasses import * def NewClass(): """Map Factory""" for cls in BaseClass.__subclasses__(): print "checking class..." class BaseClass(object): def __init__(self): print("Building an abstract BaseMap class..")
from BaseClass import BaseClass class SubClassA(BaseClass): def __init__(self): print('Instantiating SubClassA')
When I try to import BaseClass though I get the following error:
1 #import BaseClass ----> 2 from BaseClass import BaseClass 3 class SubClassA(BaseClass): 4 def __init__(self): 5 print('Instantiating SubClassA') ImportError: cannot import name BaseClass
I also tried using "import BaseClass" and then subclassing "BaseClass.BaseClass" but that resulted in a different error:
1 import BaseClass ----> 2 class SubClassA(BaseClass.BaseClass): 3 def __init__(self): 4 print('Instantiating SubClassA') AttributeError: 'module' object has no attribute 'BaseClass'
Finally, If I just try to create the subclass directory there is no problem. It is only when I try to import the BaseClass module that things go wrong.