The import statement loads the entire module into memory, so the test() function worked successfully.
But since you used the from operator, why can't you use countLines and countChars , but test can call them.
Operator
from loads the entire module and sets the imported function, variable, etc. into the global namespace.
eg,
>>> from math import sin >>> sin(90) #now sin() is a global variable in the module and can be accesed directly 0.89399666360055785 >>> math Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> math NameError: name 'math' is not defined >>> vars() #shows the current namespace, and there sin() in it {'__builtins__': <module '__builtin__' (built-in)>, '__file__': '/usr/bin/idle', '__package__': None, '__name__': '__main__', 'main': <function main at 0xb6ac702c>, 'sin': <built-in function sin>, '__doc__': None}
consider a simple file, file.py:
def f1(): print 2+2 def f2(): f1()
import only f2:
>>> from file import f2 >>> f2() 4
although I only imported f2() not f1() , but it successfully executed f1() because the module is loaded into memory, but we can only access f2() , but f2() can access other parts of the module.
source share