Modules

Modules allow you to organize your code

  • Place related functions into a file such as 'spam.py'
     # spam.py
     a = 37
     def foo():
         statements
     def bar():
         statements 
  • Use import to load and execute
     import spam
     print spam.a
     spam.foo()

Comments

  • A module defines a namespace
  • This namespace is the global namespace for everything in the module.
  • Modules are only loaded once (even for repeated import statements).
<<< O'Reilly OSCON 2000, Introduction to Python, Slide 104
July 17, 2000, beazley@cs.uchicago.edu
>>>