导入谜团 [英] import mysteries
问题描述
我对Python非常熟悉,但最近我不断发现导入的神秘问题。例如,查看
http://genshi.edgewall.org/browser/t...s/transform.py
示例使用符号''HTML''但它'没有在本地定义,它是没有明确导入的
,并且没有导入*。然而doctest将会对这个模块进行测试,并且它会以漂亮的颜色传递。结果是HTML
在genshi.input中定义。我怎么知道的?我为它而努力。
这个模块怎么可用?
另一个例子:我最近正在研究一些代码来做一个
从类方法中导入。那种进口失败了。我将
导入到文件顶部(在模块范围内)并且成功了。
我很确定没有人在使用sys.path进行修改在那个
的情况下。任何人都可以想到一个可能的解释吗?
TIA,
-
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com
Astoria研讨会== http:// www.astoriaseminar.com
I''m pretty comfortable with Python, but recently I''m constantly
finding mysterious issues with import. For example, looking at
http://genshi.edgewall.org/browser/t...s/transform.py
the examples use the symbol ''HTML'' but it''s not defined locally, it''s
not explicitly imported, and there''s no import *. Yet doctest will
test this module and it passes with flying colors. It turns out HTML
is defined in genshi.input. How do I know that? I grepped for it.
How does it become available to this module?
Another example: I was recently working on some code that did an
import from inside a class method. That import was failing. I moved
the import to the top of the file (at module scope) and it succeeded.
I''m fairly sure that nobody was monkeying around with sys.path in that
case. Can anyone think of a likely explanation?
TIA,
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com
The Astoria Seminar ==http://www.astoriaseminar.com
推荐答案
David Abrahams schrieb:
David Abrahams schrieb:
我对Python非常熟悉,但最近我不断发现导入的神秘问题。例如,查看
http://genshi.edgewall.org/browser/t...s/transform.py
示例使用符号''HTML''但它'没有在本地定义,它是没有明确导入的
,并且没有导入*。然而doctest将会对这个模块进行测试,并且它会以漂亮的颜色传递。
I''m pretty comfortable with Python, but recently I''m constantly
finding mysterious issues with import. For example, looking at
http://genshi.edgewall.org/browser/t...s/transform.py
the examples use the symbol ''HTML'' but it''s not defined locally, it''s
not explicitly imported, and there''s no import *. Yet doctest will
test this module and it passes with flying colors.
它没有通过我:
pydoctest.testmod(genshi.filters.transform)
********************************************* ***** ********************
文件" /usr/lib/python2.4/site-packages/genshi /filters/transform.py",
第29行,在genshi.filters.transform中
失败的例子:
html = HTML(''' ''''< html>
< head>< title>某些标题< / title>< / head>
< body>
一些< em> body< / emtext。
< / body>
< / html>''''''br />
异常提出:
回溯(最近一次调用最后一次):
文件" doctest.py",第1248行,__ run
compileflags,1)in test.globs
文件"< doctest genshi.filters.transform [1]>",第1行,在?
html = HTML(''''''< html>
NameError:name''HTML''不是定义
问候,
Martin
It doesn''t pass for me:
pydoctest.testmod(genshi.filters.transform)
************************************************** ********************
File "/usr/lib/python2.4/site-packages/genshi/filters/transform.py",
line 29, in genshi.filters.transform
Failed example:
html = HTML(''''''<html>
<head><title>Some Title</title></head>
<body>
Some <em>body</emtext.
</body>
</html>'''''')
Exception raised:
Traceback (most recent call last):
File "doctest.py", line 1248, in __run
compileflags, 1) in test.globs
File "<doctest genshi.filters.transform[1]>", line 1, in ?
html = HTML(''''''<html>
NameError: name ''HTML'' is not defined
Regards,
Martin
David Abrahams写道:
David Abrahams wrote:
我对Python非常熟悉,但最近我不断发现导入的神秘问题。例如,查看
http://genshi.edgewall.org/browser/t...s/transform.py
示例使用符号''HTML''但它'没有在本地定义,它是没有明确导入的
,并且没有导入*。然而doctest将会对这个模块进行测试,并且它会以漂亮的颜色传递。结果是HTML
在genshi.input中定义。我怎么知道的?我贪图它。
这个模块怎么可用?
I''m pretty comfortable with Python, but recently I''m constantly
finding mysterious issues with import. For example, looking at
http://genshi.edgewall.org/browser/t...s/transform.py
the examples use the symbol ''HTML'' but it''s not defined locally, it''s
not explicitly imported, and there''s no import *. Yet doctest will
test this module and it passes with flying colors. It turns out HTML
is defined in genshi.input. How do I know that? I grepped for it.
How does it become available to this module?
明确通过,请参阅
http://genshi.edgewall.org/browser/t...s/transform.py
另一个例子:我最近正在研究一些从类方法中导入
的代码。那种进口失败了。我将
导入到文件顶部(在模块范围内)并且成功了。
我很确定没有人在使用sys.path进行修改在那个
的情况下。谁能想到可能的解释?
Another example: I was recently working on some code that did an
import from inside a class method. That import was failing. I moved
the import to the top of the file (at module scope) and it succeeded.
I''m fairly sure that nobody was monkeying around with sys.path in that
case. Can anyone think of a likely explanation?
太模糊了,对不起。
彼得
Too vague, sorry.
Peter
David Abrahams< da ** @ boos- consulting.comwrites:
David Abrahams <da**@boost-consulting.comwrites:
我对Python非常熟悉,但最近我经常使用
发现导入的神秘问题。例如,查看
http://genshi.edgewall.org/browser/t...s/transform.py
示例使用符号''HTML''但它'没有在本地定义,它是没有明确导入的
,并且没有导入*。然而doctest将会对这个模块进行测试,并且它会以漂亮的颜色传递。结果是HTML
在genshi.input中定义。我怎么知道的?我贪图它。
这个模块怎么可用?
I''m pretty comfortable with Python, but recently I''m constantly
finding mysterious issues with import. For example, looking at
http://genshi.edgewall.org/browser/t...s/transform.py
the examples use the symbol ''HTML'' but it''s not defined locally, it''s
not explicitly imported, and there''s no import *. Yet doctest will
test this module and it passes with flying colors. It turns out HTML
is defined in genshi.input. How do I know that? I grepped for it.
How does it become available to this module?
这对我来说也是一个谜。我看不到这个''HTML''名称来自哪个模块
,我看不到这个名称来自
''genshi .input''。
无论如何解释,它都违反了
Python:名称空间的优势之一。出现在当前命名空间中的名称(因为
发生''来自foo import *'',并且好像在这里发生的事情)
是糟糕的编程风格,对于正是因为他们让
代码更难理解。
-
\" bash awk grep perl sed,df du,du-du du-du,vi troff su fsck |
` \ rm * halt LART LART LART! - 瑞典BOFH,|
_o__)alt.sysadmin.recovery |
Ben Finney
That''s a mystery to me too. I can''t see by looking at the module where
this ''HTML'' name comes from, and as you say there is no import of
''genshi.input''.
Whatever the explanation, it''s a violation of one of the strengths of
Python: namespaces. Names that appear in the current namespace (as
happens with ''from foo import *'', and as seems to be happening here)
are bad programming style, for exactly the reason that they make the
code more difficult to understand.
--
\ "bash awk grep perl sed, df du, du-du du-du, vi troff su fsck |
`\ rm * halt LART LART LART!" -- The Swedish BOFH, |
_o__) alt.sysadmin.recovery |
Ben Finney
这篇关于导入谜团的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!