Importing a function from a class in another file?

后端 未结 6 654
北恋
北恋 2020-12-05 03:55

I\'m writing a Python program for fun but got stuck trying to import a function from a class in another file. Here is my code:

#jurassic park mainframe

from         


        
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 04:39

    from otherfile import TheClass
    theclass = TheClass()
    # if you want to return the output of run
    return theclass.run()  
    # if you want to return run itself to be used later
    return theclass.run
    

    Change the end of comm system to:

    if __name__ == '__main__':
        a_game = Comm_system()
        a_game.run()
    

    It's those lines being always run that are causing it to be run when imported as well as when executed.

提交回复
热议问题