Python - 相同的代码行仅在第二次调用时才起作用?

occ*_*iso 3 python

对不起,我无法在标题中更好地描述我的问题.

我正在尝试学习Python,并遇到了这种奇怪的行为,并希望有人可以向我解释这一点.

我正在运行Ubuntu 8.10和python 2.5.2

首先我导入xml.dom
然后我创建一个minidom的实例(使用其完全qaulified名称xml.dom.minidom)
这失败,但如果我再次运行相同的行,它的工作原理!见下文:

$> python
Python 2.5.2 (r252:60911, Oct  5 2008, 19:29:17) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import xml.dom
>>> xml.dom.minidom.parseString("<xml><item/></xml>")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'minidom'
>>> xml.dom.minidom.parseString("<xml><item/></xml>")
<xml.dom.minidom.Document instance at 0x7fd914e42fc8>
Run Code Online (Sandbox Code Playgroud)

我试过另一台机器,如果一直都失败了.

jfs*_*jfs 7

这个问题是apport_python_hook.apport_excepthook()它导入的副作用xml.dom.minidom.

没有apport_except_hook:

>>> import sys
>>> sys.excepthook = sys.__excepthook__
>>> import xml.dom
>>> xml.dom.minidom
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'minidom'
>>> xml.dom.minidom
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'minidom'
>>>  
Run Code Online (Sandbox Code Playgroud)

apport_except_hook:

>>> import apport_python_hook
>>> apport_python_hook.install()
>>> xml.dom.minidom
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'minidom'
>>> xml.dom.minidom
<module 'xml.dom.minidom' from '../lib/python2.6/xml/dom/minidom.pyc'>
Run Code Online (Sandbox Code Playgroud)


use*_*019 5

minidom 是一个模块,所以你应该需要

import xml.dom.minidom
xml.dom.minidom.parseString("<xml><item/></xml>")
Run Code Online (Sandbox Code Playgroud)

我不知道你是如何让第二个 parseString 工作的,它在我的 python 上失败了,就像在你的另一台机器上一样