fibo.__name__ fib = fibo.fib fib(500) # 6.1. More on Modules from fibo import fib, fib2 fib(500) dir() from fib import * dir() fib(500) del fib,fib2 import fibo as fib fib.fib(500) import fibo dir() import importlib # changed few things in fibo.py importlib.reload(fibo) dir() # 6.1.1. Executing modules as scripts # put into fibo.py if __name__ == "__main__": import sys fib(int(sys.argv[1])) python3 fibo.py 50
import fibo print(fibo.fib(1000)) print('===============================') print(fibo.fib(100)) print('===============================') fib = fibo.fib print(fib(500)) print('===============================') from fibo import fib, fib2 print(fib(500)) print('===============================') from fibo import * print(fib(500)) print('===============================') import fibo as fib print(fib.fib(500))
del fib, fib2 # > This does not introduce the module name from which the imports are taken in the local symbol table (so in the example, fibo is not defined). # > There is even a variant to import all names that a module defines: from fibo import * fib(500) # 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 del fib, fib2 # > This imports all names except those beginning with an underscore (_). # > In most cases Python programmers do not use this facility since it introduces an unknown set of names into the interpreter, possibly hiding some things you have already defined. # > Note that in general the practice of importing * from a module or package is frowned upon, since it often causes poorly readable code. However, it is okay to use it to save typing in interactive sessions. # > If the module name is followed by as, then the name following as is bound directly to the imported module. import fibo as fib fib.fib(500) # 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 del fib # > This is effectively importing the module in the same way that import fibo will do, with the only difference of it being available as fib. # > It can also be used when utilising from with similar effects: from fibo import fib as fibonacci fibonacci(500) # 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 # > Note For efficiency reasons, each module is only imported once per interpreter session. # > Therefore, if you change your modules, you must restart the interpreter – or, if it’s just one module you want to test interactively, use importlib.reload(), e.g. import importlib; importlib.reload(modulename).
from fibo import fib fib.fib(10)
This imports all names except those beginning with an underscore (_). In most cases Python programmers do not use this facility since it introduces an unknown set of names into the interpreter, possibly hiding some things you have already defined. Note that in general the practice of importing * from a module or package is frowned upon, since it often causes poorly readable code. However, it is okay to use it to save typing in interactive sessions. """ from fibo import * fib(600) # If the module name is followed by as, then the name following as is # bound directly to the imported module. import fibo as fib fib.fib(30) # Can also use as with from from fibo import fib as fibonacci fibonacci(500) """ Note For efficiency reasons, each module is only imported once per interpreter session. Therefore, if you change your modules, you must restart the interpreter – or, if it’s just one module you want to test interactively, use importlib.reload(), e.g. import importlib; importlib.reload(modulename). """ # Made a change to fibo.py import fibo # Changes not reflected