Extended import Statement

Another fairly common request from Python programmers is the ability to import modules and module attributes into your program using names other than their original given names. One common workaround is to assign the module name to a variable:

>>> import longmodulename
>>> short = longmodulename
>>> del longmodulename

In the example above, rather than using longmodulename.attribute, you would use the short.attribute to access the same object. (A similar analogy can be made with importing module attributes using from-import… see below.) However, to do this over and over again, and in multiple modules can be annoying and seem wasteful. The new extended import statement will now support the following:

>>> import longmodulename as short

Accordingly, you may also use this syntax with from-import statements.

>>> from sys import stderr as err
>>> err.write("now using sys.stderr")
now using sys.stderr

We will note that as is not a new keyword and is only recognized when using import. As a result, you can still use it as a valid identifier in your code:

>>> as = 14
>>> as += 3
>>> as
17

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset