how to import only the class methods in python

后端 未结 5 1996
梦谈多话
梦谈多话 2020-12-19 04:50

I have a GP.py file that I am then running a MyBot.py file from.

In the MyBot.py file, I have the line

from GP import *

I have a su

5条回答
  •  Happy的楠姐
    2020-12-19 05:30

    You cannot import class methods separately, you have to import the classes. You can do this by enumerating the classes you want to import:

    from GP import class1, class2, class3
    

    Note that this will still load the entire module. This always happens if you import anything from the module. If you have code in that module that you do not want to be executed when the module is imported, you can protect it like this:

    if __name__ == "__main__":
        # put code here
    

    Code inside the block will only be executed if the module is run directly, not if it is imported.

提交回复
热议问题