验证 cElementTree 或 elementTree 节点类型

Verifying cElementTree or elementTree node type

我在我的应用程序中使用 cElementTree 解析 XML 文件并且它工作得很好,但是,现在我想写一些单元测试来检查我的方法的结果是否 return 预期的结果.

在我的一个测试中,我想检查从树中检索到的元素是否确实是元素类型:

self.assertIsInstance(myElem, Element, 'Incorrect type for myElem')

当我执行测试时,出现以下错误:

TypeError: isinstance() arg 2 must be a class, type or tuple of classes and types.

如果 Element 不是 class 或类型,那它又是什么?我在文档中找不到很多关于此的详细信息。

我从源代码中找到了这段摘录, 文件:ElementTree.py

##
# Element factory.  This function returns an object implementing the
# standard Element interface.  The exact class or type of that object
# is implementation dependent, but it will always be compatible with
# the {@link #_ElementInterface} class in this module.
# <p>
# The element name, attribute names, and attribute values can be
# either 8-bit ASCII strings or Unicode strings.
#
# @param tag The element name.
# @param attrib An optional dictionary, containing element attributes.
# @param **extra Additional attributes, given as keyword arguments.
# @return An element instance.
# @defreturn Element

def Element(tag, attrib={}, **extra):
    attrib = attrib.copy()
    attrib.update(extra)
    return _ElementInterface(tag, attrib)

所以,Element归根结底是一个工厂方法。