bpo-31454: Include information about "import X as Y" in Modules tutor… · python/cpython@e48e649

Original file line numberDiff line numberDiff line change

@@ -112,6 +112,25 @@ Note that in general the practice of importing ``*`` from a module or package is

112112

frowned upon, since it often causes poorly readable code. However, it is okay to

113113

use it to save typing in interactive sessions.

114114
115+

If the module name is followed by :keyword:`as`, then the name

116+

following :keyword:`as` is bound directly to the imported module.

117+
118+

::

119+
120+

>>> import fibo as fib

121+

>>> fib.fib(500)

122+

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

123+
124+

This is effectively importing the module in the same way that ``import fibo``

125+

will do, with the only difference of it being available as ``fib``.

126+
127+

It can also be used when utilising :keyword:`from` with similar effects::

128+
129+

>>> from fibo import fib as fibonacci

130+

>>> fibonacci(500)

131+

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

132+
133+
115134

.. note::

116135
117136

For efficiency reasons, each module is only imported once per interpreter