回首忆惘然
来自ElementTree的源代码(属于 类Element):class Element:[...] def __init__(self, tag, attrib={}, **extra): if not isinstance(attrib, dict): raise TypeError("attrib must be dict, not %s" % ( attrib.__class__.__name__,)) self.tag = tag self.attrib = {**attrib, **extra} self._children = [][...] def __bool__(self): warnings.warn( "The behavior of this method will change in future versions. " "Use specific 'len(elem)' or 'elem is not None' test instead.", FutureWarning, stacklevel=2 ) return len(self._children) != 0 # emulate old behaviour, for now由于您刚刚初始化了一个新元素,因此它_children是一个空列表(如 中所示__init__),因此如果您了解失败的原因,您也assert []应该了解失败的原因。assert xml_element