Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
136225c2
Commit
136225c2
authored
Dec 04, 1998
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add explicit example on how to import a submodule of a package using
__import__ and getattr().
parent
72c2c87d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
0 deletions
+15
-0
Doc/lib/libfuncs.tex
Doc/lib/libfuncs.tex
+15
-0
No files found.
Doc/lib/libfuncs.tex
View file @
136225c2
...
...
@@ -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
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment