Chapter 15-18 Modules. CSC1310 Fall 2009. Modules. Modules are the highest level program organization unit, usually correspond to source files and serve as libraries of tools. Each file is a module and modules import other modules to use the name they define.
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
CSC1310 Fall 2009
1. Code reuse
Code in module is persistent: can be reloaded and run as many times as needed.
2. System namespace partitioning
Everything “lives” in a module: code and objects are always implicitly enclosed by a module.
3. Implementing shared services or data
Components shared across a system
>>>import math # file to be loaded
>>>print math.pi # variable in the script
>>>print math.sqrt(4), math.log(10)
>>>from math import pi, sqrt
>>>from fib import *
>>>print fibTopN(6), fib(6), list, add()
>>>from fib import list, size
>>>fib.size=23 #earlier “size” was 12 which is still 12
>>>import fib as f
>>>from fib import multiple as mult
return [l[x]*x for x in range(len(l))]
>>>from math import sqrt,pi
>>>reload(math) # doesn’t work, from assigned
name pi and sqrt, not name math
>>>print pi # from makes copy of function!
print “It is imported”