I have a file called main.py and a file called classes.py
main.py contains the application and what’s happening while class.py contains some classes.
main.py has the following code
main.py
import classes def addItem(text): print text myClass = classes.ExampleClass()
And then we have classes.py
classes.py
class ExampleClass (object): def __init__(self): addItem('bob')
Surprisingly enough that’s not the actual code I am using because I’ve stripped out anything that’d get in the way of you seeing what I want to do. I want to be able to call a method that’s defined in main.py from a class within classes.py. How do I do this?
Thanks in advance
I couldn’t answer this any better than this post by Alex Martelli. Basically any way you try to do this will lead to trouble and you are much better off refactoring the code to avoid mutual dependencies between two modules…
If you have two modules A and B which depend on each other, the easiest way is to isolate a part of the code that they both depend on into a third module C, and have both of them import C.