summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1998-12-04 15:32:17 (GMT)
committerGuido van Rossum <guido@python.org>1998-12-04 15:32:17 (GMT)
commit8c2da61811e3f6aa50859d656b7c2f0ac58226f3 (patch)
treec7697d28504d766717b8e369a68ac925a2288ff5
parentce0bbd270b6f8ae82d3e164c0d95a99a0c1c235c (diff)
downloadcpython-8c2da61811e3f6aa50859d656b7c2f0ac58226f3.zip
cpython-8c2da61811e3f6aa50859d656b7c2f0ac58226f3.tar.gz
cpython-8c2da61811e3f6aa50859d656b7c2f0ac58226f3.tar.bz2
Add explicit example on how to import a submodule of a package using
__import__ and getattr().
-rw-r--r--Doc/lib/libfuncs.tex15
1 files changed, 15 insertions, 0 deletions
diff --git a/Doc/lib/libfuncs.tex b/Doc/lib/libfuncs.tex
index d7ee837..466351d 100644
--- a/Doc/lib/libfuncs.tex
+++ b/Doc/lib/libfuncs.tex
@@ -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}