lxml.objectify 似乎没有为我的自定义元素类调用构造函数:
from lxml import objectify, etree
class CustomLookup(etree.CustomElementClassLookup):
def lookup(self, node_type, document, namespace, name):
lookupmap = { 'custom' : CustomElement }
try:
return lookupmap[name]
except KeyError:
return None
class CustomElement(etree.ElementBase):
def __init__(self):
print("Made CustomElement")
parser = objectify.makeparser()
parser.set_element_class_lookup(CustomLookup())
root = objectify.parse(fname,parser).getroot()
假设正在解析的文件是
<custom />
我希望它打印“Made CustomElement”,但事实并非如此。我可以让它调用构造函数吗?
如何在不调用构造函数的情况下创建 CustomElement 类的实例?
>>> isinstance(root,CustomElement)
True