Commit 136225c2 authored by Guido van Rossum's avatar Guido van Rossum

Add explicit example on how to import a submodule of a package using

__import__ and getattr().
parent 72c2c87d
......@@ -44,6 +44,21 @@ using \samp{import spam.ham.eggs}, the top-level package \code{spam}
must be placed in the importing namespace, but when using \samp{from
spam.ham import eggs}, the \code{spam.ham} subpackage must be used to
find the \code{eggs} variable.
As a workaround for this behavior, use \function{getattr()} to extract
the desired components. For example, you could define the following
helper:
\begin{verbatim}
import string
def my_import(name):
mod = __import__(name)
components = string.split(name, '.')
for comp in components[1:]:
mod = getattr(mod, comp)
return mod
\end{verbatim}
\end{funcdesc}
\begin{funcdesc}{abs}{x}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment