Проверка типа узла cElementTree или elementTree

Я использовал cElementTree для анализа файлов XML в своем приложении, и он прекрасно работает, однако теперь я хотел написать несколько модульных тестов, чтобы проверить, возвращают ли результаты моих методов ожидаемые результаты.

В одном из моих тестов я хочу проверить, действительно ли извлеченный элемент из дерева является типом элемента:

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

Когда я выполняю свой тест, я получаю следующую ошибку:

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

Что такое Element, если это не класс или тип? Я не смог найти много подробностей об этом в документации.


person Spektor    schedule 22.12.2015    source источник


Ответы (1)


Я нашел этот отрывок из исходного кода, Файл: 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 в конечном счете является фабричным методом.

person Aashish P    schedule 22.12.2015