开发者

splitting one program into several smaller and their binding in python?

i want to make from one big program 5 smaller programs:main and program开发者_如何学运维1,program2,program3 and program4.programs1,2,3,4 should use variables from main program and return some new variables,and main program should use(or call) programs1,2,3,4... can i bind these programs using functions,modules or something else and how? i'm new in Python and any help will be usefull


You can simply use functions to do this:

def function1(a):
    print a

def function2(b):
    print b

def function3(c):
    print c

def function4():
    return "hello!"

def main():
    a, b, c = (1, 2, 3)
    function1(a)
    function2(b)
    function3(c)
    d = function4()
    print d

if __name__ == "__main__":
    main()

Or you can put the definitions of the functions in a seperate file, say functions.py and use import functions in your main program file.

import functions

def main():
    a, b, c = (1, 2, 3)
    functions.function1(a)
    functions.function2(b)
    functions.function3(c)
    d = functions.function4()
    print d

if __name__ == "__main__":
    main()

This should be enough to get you started. I would recommend Dive Into Python as a good learning reference if you have some previous experience programming and want to learn more about Python.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜