Three python modules, calling one another -


i working on project have 3 python modules (a.py, b.py , c.py).

module a calling module b, module b calling module c, , module c calling module a. behaviour bizzare when runs.

here 3 modules:

a.py

print('module a') def a() :      print('inside a')     return true  import b b.b() 

b.py

print('module b')  def b() :      print('inside b')     return true  import c  c.c() 

c.py

print('module c')  def c() :      print('inside c')     return true  import  a.a() 

when run a.py, output observed :

module module b module c module inside b inside inside c inside b 

whereas expected behavior is:

module module b module c module inside b 

why happen? there alternative way such implementation?

this has stack frames , how functions , imports called.

you start running a.py.

'module a' 

first thing happens: import b:

'module b' 

within b, c.py imported:

'module c' 

module c imports a, , get:

'module a' 

b has been imported running a.py in first place, call of import b passed (we not re-import b.py). see effects of calling b.b(), next statement:

inside b 

and return c.py's frame, call a.a():

inside 

c.py has run course; next jump b.py, left off (right after importing), , call c.c():

inside c 

now b.py has finished running well. return a.py frame ran program, , call b.b():

inside b 

hope helps explain behavior. here's example of how rectify problem , desired output:

a.py:

print("module a") import b  def main():     b.b()  def a():     print("inside a")  if __name__ == "__main__":     main() 

b.py:

print("module b") import c  def main():     c.c()  def b():     print("inside b")  if __name__ == "__main__":     main() 

c.py:

print("module c") import  def main():     a.a()  def c():     print("inside c")  if __name__ == "__main__":     main() 

here's link explain happens if __name__ == "__main__": main() call. run main() function each script if script built & run in first place. in way, desired output:

module module b module c module inside b 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -