[issue15575] Tutorial is unclear on multiple imports of a module.

New submission from Roy Smith: Opening this bug at Ben Finney's request. See https://groups.google.com/forum/?fromgroups#!topic/comp.lang.python/wmDUrpW2... for the full thread discussing the problem. Here's a significant excerpt: ------------------------------------------------- The tutorial is misleading on this. It it says plainly: A module can contain executable statements as well as function definitions. […] They are executed only the *first* time the module is imported somewhere. <URL:http://docs.python.org/tutorial/modules.html> but it doesn't make clear that a module can exist in the ‘sys.modules’ list multiple times under different names. ------------------------------------------------- Also note: -------------------------------------------------- The footnote to that is wrong too:
[1] In fact function definitions are also ‘statements’ that are ‘executed’; the execution of a module-level function enters the function name in the module’s global symbol table.
I think what it's supposed to say is "... the execution of a module-level def statement ..." --------------------------------------------------- ---------- assignee: docs@python components: Documentation messages: 167628 nosy: docs@python, roysmith priority: normal severity: normal status: open title: Tutorial is unclear on multiple imports of a module. type: enhancement versions: Python 2.7 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue15575> _______________________________________

R. David Murray added the comment: Well, I don't think a full discussion of the subtlety about a module appearing under multiple names belongs in the tutorial, but I think the sentence could be amended to say "Statements in a module are executed only the *first* time the module name is encountered in an import statement". The other is indeed a bug (IMO the sentence is missing the word 'definition', as in 'execution of a function definition'). ---------- nosy: +r.david.murray _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue15575> _______________________________________

Changes by Ezio Melotti <ezio.melotti@gmail.com>: ---------- keywords: +easy nosy: +ezio.melotti stage: -> needs patch _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue15575> _______________________________________

jamesob added the comment: Howdy; attached is a patch containing the doc changes. I figured that some elaboration was in addition to what r.david.murray recommended, so I've included a brief allusion to the problem of equivalent paths. ---------- keywords: +patch nosy: +jamesob Added file: http://bugs.python.org/file29575/module_multi_exec_doc_27.patch _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue15575> _______________________________________

Roundup Robot added the comment: New changeset 9df9931fae96 by R David Murray in branch '3.3': #15575: Clarify tutorial description of when modules are executed. http://hg.python.org/cpython/rev/9df9931fae96 New changeset dac847938326 by R David Murray in branch 'default': #15575: Clarify tutorial description of when modules are executed. http://hg.python.org/cpython/rev/dac847938326 New changeset a1421d28393b by R David Murray in branch '2.7': #15575: Clarify tutorial description of when modules are executed. http://hg.python.org/cpython/rev/a1421d28393b ---------- nosy: +python-dev _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue15575> _______________________________________

R. David Murray added the comment: Thanks, James. I wound up going with a different wording for the "elaboration": since the concept of running a python file as a script is mentioned just a bit earlier, I added a parenthetical that the statements are also executed if the module is run as a script. ---------- resolution: -> fixed stage: needs patch -> committed/rejected status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue15575> _______________________________________
participants (5)
-
Ezio Melotti
-
jamesob
-
R. David Murray
-
Roundup Robot
-
Roy Smith