Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to import only the class methods in python

Tags:

python

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 suspicion it is importing the whole file instead of just the class methods and class descriptions I want. In the GP.py file, There is code in addition to the defintions

like image 222
SwimBikeRun Avatar asked Dec 14 '11 09:12

SwimBikeRun


1 Answers

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.

like image 181
Björn Pollex Avatar answered Oct 02 '22 01:10

Björn Pollex