class - Call a function in main file from module using python -


i trying make python script works so:

i've got main file import file, class functions inside it. main file calls function in module, here works well, when def inside module calls def in main again, error.

for example, in main.py i've got this

from module import * module = example()  def foo():  module.foo2()  def foo3():  print "here problem"  if(__name__ == "__main__"):  foo() 

in module.py i've got:

class example():  def foo2(self):   foo3() 

foo2 called perfectly, when try call foo3() nameerror (global name 'foo3' not defined). know should import somehow don't know how it.

last thing: quite new python please explain :-)

i avoid creating circular dependencies , refactor code such foo3 either in module, or new module. example (i cleaned things little bit went along follow pep8):

main.py

import module example = module.example()  def foo():  module.foo2()  if __name__ == "__main__":  foo() 

module.py

import module3  class example():     def foo2(self):         module3.foo3() 

module3.py

 def foo3():      print "here problem" 

if absolutely must keep circular dependancy, best way handle move import in module.py end of file suggested on effbot. again, avoid doing @ cost.

class example():  def foo2(self):   main.foo3()  import main 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -