Reputation: 731
Suppose I have this folder structure:
module
module.py
__init__.py
main.py
Main.py imports module.py which itself should have functions that are only present in main.py. For example, main.py code:
from module import *
def foo(var):
print var
module.foo_module()
Content of module.py:
def foo_module():
foo("Hello world!")
Is there anyway I can achieve this without repeating the functions? If not, how can I import main.py into module.py?
Many thanks
Upvotes: 0
Views: 59
Reputation: 38462
Everything is an object in python, including functions. You can pass the necessary function as an argument. Whether this makes sense in your case, I don't have enough information to know.
def foo(var):
print var
module.foo_module(foo)
def foo_module(foo):
foo("Hello world!")
Upvotes: 1
Reputation: 879083
Avoid circular imports. You could do that here by placing foo
in module
.
If you don't want foo
in module
, you could instead create a separate module bar
to hold foo
, and import bar
in both main
and module
.
Upvotes: 0