Call a function from another file?

前端 未结 17 2771
梦谈多话
梦谈多话 2020-11-22 08:06

Set_up: I have a .py file for each function I need to use in a program.

In this program, I need to call the function from the external files.

I\'ve tried:

17条回答
  •  天命终不由人
    2020-11-22 09:09

    Suppose the file you want to call is anotherfile.py and the method you want to call is method1, then first import the file and then the method

    from anotherfile import method1
    

    if method1 is part of a class, let the class be class1, then

    from anotherfile import class1
    

    then create an object of class1, suppose the object name is ob1, then

    ob1 = class1()
    ob1.method1()
    

提交回复
热议问题