Note that there are some explanatory texts on larger screens.

plurals
  1. POPython class factory problem referencing base class?
    primarykey
    data
    text
    <p>I recently came across a <a href="https://stackoverflow.com/questions/456672/class-factory-in-python">Python class factory implementation</a> 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.</p> <p>When I try to do this, however, I run into a problem whenever I try to load the base class.</p> <p>Structure:</p> <p><strong>BaseClass.py</strong></p> <pre><code>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..") </code></pre> <p><strong>subclasses/__init__.py</strong></p> <pre><code>__all__=['SubClass'] </code></pre> <p><strong>subclasses/SubClass.py</strong></p> <pre><code>from BaseClass import BaseClass class SubClassA(BaseClass): def __init__(self): print('Instantiating SubClassA') </code></pre> <p>When I try to import BaseClass though I get the following error:</p> <pre> 1 #import BaseClass ----> 2 from BaseClass import BaseClass 3 class SubClassA(BaseClass): 4 def __init__(self): 5 print('Instantiating SubClassA') <b>ImportError: cannot import name</b> BaseClass </pre> <p>I also tried using "import BaseClass" and then subclassing "BaseClass.BaseClass" but that resulted in a different error:</p> <pre> 1 import BaseClass ----> 2 class SubClassA(BaseClass.BaseClass): 3 def __init__(self): 4 print('Instantiating SubClassA') <b>AttributeError: 'module' object has no attribute 'BaseClass'</b> </pre> <p>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.</p> <p>Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload