DekGenius.com
[ Team LiB ] Previous Section Next Section

18.5 The import as Extension

Both the import and from statements have been extended to allow a module to be given a different name in your script:

import longmodulename as name

is equivalent to:

import longmodulename
name = longmodulename
del longmodulename          # Don't keep original name.

After the import, you can (and in fact must) use the name after the as to refer to the module. This works in a from statement too:

from module import longname as name

to assign the name from the file to a different name in your script. This extension is commonly used to provide short synonyms for longer names, and to avoid name clashes when you are already using a name in your script that would otherwise be overwritten by a normal import statement. This also comes in handy for providing a short, simple name for an entire directory path, when using the package import feature described in Chapter 17.

    [ Team LiB ] Previous Section Next Section