lxml objectify 不会调用自定义元素类的构造函数
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
lxml.objectify does not seem to call the constructors for my custom element classes:
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()
Suppose the file being parsed is
<custom />
I would like this to print "Made CustomElement", but it does not. Can I make it call the constructor?
How is it possible for an instance of the CustomElement class to be created without the constructor being called?
>>> isinstance(root,CustomElement)
True
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自
lxml
文档:From the
lxml
docs: