Movatterモバイル変換


[0]ホーム

URL:


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:3.21 impUp:3.21 impNext:3.22 code

 
3.21.1 Examples

The following function emulates what was the standard import statementup to Python 1.4 (no hierarchical module names). (Thisimplementation wouldn't work in that version, sincefind_module() has been extended andload_module() has been added in 1.4.)

import impimport sysdef __import__(name, globals=None, locals=None, fromlist=None):    # Fast path: see if the module has already been imported.    try:        return sys.modules[name]    except KeyError:        pass    # If any of the following calls raises an exception,    # there's a problem we can't handle -- let the caller handle it.    fp, pathname, description = imp.find_module(name)        try:        return imp.load_module(name, fp, pathname, description)    finally:        # Since we may exit via an exception, close fp explicitly.        if fp:            fp.close()

A more complete example that implements hierarchical module names andincludes areload() function can befound in the moduleknee .


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:3.21 impUp:3.21 impNext:3.22 code
Release 2.2.3, documentation updated on 30 May 2003.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2026 Movatter.jp