【问题标题】:difference between `attrib` and `extra` arguments of xml.etree.ElementTree.Element(tag, attrib={}, **extra)xml.etree.ElementTree.Element(tag, attrib={}, **extra) 的 `attrib` 和 `extra` 参数之间的区别
【发布时间】:2017-01-23 15:30:55
【问题描述】:

xml.etree.ElementTree.Element(tag, attrib={}, **extra)attribextra 参数都可以用来设置元素属性,除了attrib 应该作为字典和extra 作为关键字参数,它们几乎具有相同的效果,例如,

>>> from xml.etree.ElementTree import *
>>> tostring(Element('ref', attrib={'text1': 'KCTV'}), 'utf-8')
b'<ref text1="KCTV" />'
>>> tostring(Element('ref',text1= 'KCTV'), 'utf-8')
b'<ref text1="KCTV" />'
>>> 

我看不出两者之间有什么区别,(1) 有什么区别吗? (2) 每个参数的用途是什么?

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    看源码,它们是合并在一起的:

    attrib.update(extra)
    

    https://hg.python.org/cpython/file/tip/Lib/xml/etree/ElementTree.py#l172

    自从 ElementTree 是支持 Python 1.5.2 的第三方库以来,它就一直存在。

    人们会认为它是为了方便起见,因为从某个地方拥有一个属性字典可能很常见,但编写带有一些属性名称的文字函数调用也应该符合人体工程学,因此这两种情况都不需要额外的标点符号.

    【讨论】:

      猜你喜欢
      • 2012-04-21
      • 2021-12-08
      • 2013-08-25
      • 2011-03-01
      • 2021-12-23
      • 1970-01-01
      • 2013-01-04
      • 1970-01-01
      • 2011-03-11
      相关资源
      最近更新 更多