summaryrefslogtreecommitdiffstats
path: root/Doc/faq/programming.rst
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2014-10-06 14:02:09 (GMT)
committerGeorg Brandl <georg@python.org>2014-10-06 14:02:09 (GMT)
commita94ad1e508153cee6d355a1ee03e32146fa0c41f (patch)
treea4e1538b3a2cdc67cfa0440af8e3ac92646ca4e5 /Doc/faq/programming.rst
parent2a3e396b247e8519b8f8a7c9d68f418a0fe0a306 (diff)
downloadcpython-a94ad1e508153cee6d355a1ee03e32146fa0c41f.zip
cpython-a94ad1e508153cee6d355a1ee03e32146fa0c41f.tar.gz
cpython-a94ad1e508153cee6d355a1ee03e32146fa0c41f.tar.bz2
Closes #10031: overhaul the "imports" section of the programming FAQ.
Remove the advice to never use relative imports; it is a leftover from 2.x implicit relative imports. Remove the advice to locally import modules in __init__, it is a strange practice. Remove the advice to use "from ... import *" with some modules.
Diffstat (limited to 'Doc/faq/programming.rst')
-rw-r--r--Doc/faq/programming.rst18
1 files changed, 2 insertions, 16 deletions
diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst
index ffb7e8b..c30c2b6 100644
--- a/Doc/faq/programming.rst
+++ b/Doc/faq/programming.rst
@@ -292,9 +292,8 @@ What are the "best practices" for using import in a module?
-----------------------------------------------------------
In general, don't use ``from modulename import *``. Doing so clutters the
-importer's namespace. Some people avoid this idiom even with the few modules
-that were designed to be imported in this manner. Modules designed in this
-manner include :mod:`tkinter`, and :mod:`threading`.
+importer's namespace, and makes it much harder for linters to detect undefined
+names.
Import modules at the top of a file. Doing so makes it clear what other modules
your code requires and avoids questions of whether the module name is in scope.
@@ -308,11 +307,6 @@ It's good practice if you import modules in the following order:
directory) -- e.g. mx.DateTime, ZODB, PIL.Image, etc.
3. locally-developed modules
-Never use relative package imports. If you're writing code that's in the
-``package.sub.m1`` module and want to import ``package.sub.m2``, do not just
-write ``from . import m2``, even though it's legal. Write ``from package.sub
-import m2`` instead. See :pep:`328` for details.
-
It is sometimes necessary to move imports to a function or class to avoid
problems with circular imports. Gordon McMillan says:
@@ -343,14 +337,6 @@ module, but loading a module multiple times is virtually free, costing only a
couple of dictionary lookups. Even if the module name has gone out of scope,
the module is probably available in :data:`sys.modules`.
-If only instances of a specific class use a module, then it is reasonable to
-import the module in the class's ``__init__`` method and then assign the module
-to an instance variable so that the module is always available (via that
-instance variable) during the life of the object. Note that to delay an import
-until the class is instantiated, the import must be inside a method. Putting
-the import inside the class but outside of any method still causes the import to
-occur when the module is initialized.
-
Why are default values shared between objects?
----------------------------------------------