Skip to Content
Python: Essential Reference, Third Edition
book

Python: Essential Reference, Third Edition

by David Beazley
February 2006
Intermediate to advanced content levelIntermediate to advanced
648 pages
14h 53m
English
Sams
Content preview from Python: Essential Reference, Third Edition

Modules

You can turn any valid source file into a module by loading it with the import statement. For example, consider the following code:

# file : spam.py
a = 37                    # A variable
def foo:                  # A function
    print "I'm foo"
class bar:                # A class
    def grok(self):
        print "I'm bar.grok"
b = bar()                 # Create an instance

To load this code as a module, you use the statement import spam. The first time import is used to load a module, it does three things:

  1. It creates a new namespace that serves as a namespace to all the objects defined in the corresponding source file. This is the namespace accessed when functions and methods defined within the module use the global statement.

  2. It executes the code contained in the module within the newly created namespace.

  3. It creates ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python: Essential Reference

Python: Essential Reference

David M. Beazley

Publisher Resources

ISBN: 0672328623Purchase book