Reputation: 1545
Supposing I have written a set of classes to be used in a python file and use them in a script (or python code in a different file). Now both the files require a set of modules to be imported. Should the import be included only once, or in both the files ?
File 1 : my_module.py.
import os
class myclass(object):
def __init__(self,PATH):
self.list_of_directories = os.listdir(PATH)
File 2 :
import os
import my_module
my_module.m = myclass("C:\\User\\John\\Desktop")
list_ = m.list_of_directories
print os.getcwd()
Should I be adding the line import os
to both the files ?
How does this impact the performance, supposing there are lots of modules to be imported ? Also, is a module ,once imported, reloaded in this case ?
Upvotes: 7
Views: 1499
Reputation: 2590
Each file that you are using a module in, must import that module. Each module is its own namespace. Things you explicitly import within that file are available in that namespace. Thus, if you need os
in both files, you should import them in both files.
Upvotes: 7