Section 12.6. Module Built-in Functions


12.6. Module Built-in Functions

The importation of modules has some functional support from the system. We will look at those now.

12.6.1. __import__ ()

The __import__() function is new as of Python 1.5, and it is the function that actually does the importing, meaning that the import statement invokes the __import__() function to do its work. The purpose of making this a function is to allow for overriding it if the user is inclined to develop his or her own importation algorithm.

The syntax of __import__() is:

__import__(module_name[, globals[, locals[, fromlist]]])


The module_name variable is the name of the module to import, globals is the dictionary of current names in the global symbol table, locals is the dictionary of current names in the local symbol table, and fromlist is a list of symbols to import the way they would be imported using the from-import statement.

The globals, locals, and fromlist arguments are optional, and if not provided, default to globals(), locals(), and [], respectively.

Calling import sys can be accomplished with

sys = __import__('sys')


12.6.2. globals() and locals()

The globals() and locals() built-in functions return dictionaries of the global and local namespaces, respectively, of the caller. From within a function, the local namespace represents all names defined for execution of that function, which is what locals() will return. globals(), of course, will return those names globally accessible to that function.

From the global namespace, however, globals() and locals() return the same dictionary because the global namespace is as local as you can get while executing there. Here is a little snippet of code that calls both functions from both namespaces:

def foo():     print '\ncalling foo()...'     aString = 'bar'     anInt = 42     print "foo()'s globals:", globals().keys()     print "foo()'s locals:", locals().keys() print "__main__'s globals:", globals().keys() print "__main__'s locals:", locals().keys() foo()


We are going to ask for the dictionary keys only because the values are of no consequence here (plus they make the lines wrap even more in this text). Executing this script, we get the following output:

$ namespaces.py __main__'s globals: ['__doc__', 'foo', '__name__', '__builtins__'] __main__'s locals: ['__doc__', 'foo', '__name__', '__builtins__'] calling foo()... foo()'s globals: ['__doc__', 'foo', '__name__', '__builtins__'] foo()'s locals: ['anInt', 'aString']


12.6.3. reload()

The reload() built-in function performs another import on a previously imported module. The syntax of reload() is:

reload(module)


module is the actual module you want to reload. There are some criteria for using the reload() module. The first is that the module must have been imported in full (not by using from-import), and it must have loaded successfully. The second rule follows from the first, and that is the argument to reload() the module itself and not a string containing the module name, i.e., it must be something like reload(sys) instead of reload('sys').

Also, code in a module is executed when it is imported, but only once. A second import does not re-execute the code, it just binds the module name. Thus reload() makes sense, as it overrides this default behavior.



Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net