2

I have a module which can be described as python

class Symbol():
    def __init__(data):
        self.data = data
        pass
    def __add__(self,other):
        return Add(self,other)

class Integer(Symbol):
    pass

class Add(Symbol):
    def __init__(a,b):
        self.data = [a,b]

I want to split it into three files, which are symbol.py, integer.py and add.py; there are of course going to be a lot more details on those classes so having them in one files is ridiculous.

For some reason the imports never seem to work, while it's not even complaining of circular dependencies, can someone give me a little example?

Oisin
  • 731
  • 8
  • 19
Onza
  • 1,340
  • 2
  • 13
  • 31
  • Did you use `from add import Add`? (Assuming you creared add.py) – Oisin Jan 17 '16 at 23:25
  • I tried relative as well as absolute imports, none of them seemed to work for me, since I tried a lot, the amount of tracebacks is insane, I just would like some sort of working example to built on. – Onza Jan 17 '16 at 23:30
  • 1
    Sounds like you've turned your module into a package. Did you put it into a suitable subdirectory? More importantly, make sure you have an `__init__.py` file in that subdirectory. –  Jan 17 '16 at 23:31
  • 1
    It would be easier to come up with suggestions if you were to provide more details about file names, folder structure, code you've tried, etc. (What is the name of this file? What folder is it in? What is the location of the code you're writing that's failing? Etc.) – aldo Jan 18 '16 at 00:00
  • I just actually misclicked post, I got an emergency while I was writting and didn't finish it, maybe I should delete this question. – Onza Jan 18 '16 at 00:14

1 Answers1

1

Your circular dependency situation isn't unsolvable, because Symbol doesn't depend on Add at definition time, only when the __add__ method is called. There are two good ways to resolve it.

The first is to not have the module Symbol is in import Add at top level, but only do that within the __add__ method itself. For instance, if your modules were named after the classes (only lowercase), you'd use this in symbol.py:

class Symbol():
    # ...

    def __add__(self,other):
        from add import Add
        return Add(self,other)

The other approach is to import Add globally into the symbol module, but do so after the definintion of the Symbol class. This way, when the add module imports symbol back, it will always be able to see the Symbol class's definition, even if the rest of the module is not finished loading.

class Symbol():
    # same as in your current code

from add import Add

If you go with this second approach and the Symbol class imports other stuff at the top of the file (where import statements normally are put), you might want to add a comment in that space about Add being imported later (and why).

Blckknght
  • 85,872
  • 10
  • 104
  • 150
  • thanks man, I'm surprised you could do all that without me being able to make a proper definition due to my emergency. – Onza Jan 18 '16 at 12:01
  • I also have a quesiton, wouldn't the first approach cause my code to load `from add import Add` several times? causing it to be slow? – Onza Jan 18 '16 at 12:04
  • The `from add import Add` line will run repeatedly, but it will be fast, since the `add` module will be cached after the first time it is imported. The second and all subsequent imports will only need to look it up in `sys.modules`, not load it again. – Blckknght Jan 18 '16 at 17:10