Calling functions from other files

Well, I’ve solved this problem by myself. I just had to move the external classes declarations outside of the local class, just after the import statements, in all files.

Example :

import getopt,sys
import py2,py3,py4

myPy2 = py2.class2()
myPy3 = py3.class3()
myPy4 = py4.class4()

class class1:

    def function1(self):
        myPy4.function4('from py1')
        myPy3.function3()
        myPy2.function2()

    def describe_options(self):
        print ('Usage:')
        print ('-v|--verbose: enable verbose mode')
        print ('-h|--help: displays this help')


if __name__ == '__main__':
    myPy1 = class1()
    myPy2 = py2.class2()
    myPy3 = py3.class3()
    myPy4 = py4.class4()
    
    try:
        opts, args = getopt.getopt(sys.argv[1:], "vh", ["verbose", "help"])
    except getopt.GetoptError as err:
        print (str(err))  # will print something like "option -a not recognized"
        print ("Using -h or --help might save you some time...")
        sys.exit(2)

    for the_option, the_arg in opts:
        # print "->"+the_option
        if the_option in ("-v", "--verbose"):
            myPy4.set_verbose(True)
        elif the_option in ("-h", "--help"):
            myPy1.describe_options()
            sys.exit(1)
        else:
            assert False, "unhandled option"
  
    myPy1.function1()

Result with all above files modified :

Have a happpy and safe new year’s eve!

1 Like