Two ElementTree questions

Fredrik Lundh fredrik at pythonware.com
Fri Apr 28 01:41:01 EDT 2006


mirandacascade at yahoo.com wrote:

> 1) It appears as if the following logic works for determining whether
> an element is a parent:
>
> # assume elem is an ElementTree element
> if (elem.getchildren() == None):
>     print 'this element is not a parent'
> else:
>     print 'this element is a parent'
>
> My question is this: are there any other ways of determining whether an
> element is a parent, and if so, are they preferable to the method
> above?

if len(elem):
    print "is a parent"
else:
    print "is not a parent (has no children)"

(in 1.2.X, you can also write "if elem", but that use is discouraged.
see http://effbot.org/zone/element.htm#the-element-type for more
on this).

> 2) At one time, I thought I saw some notes indicating that the
> getchildren() method will be deprecated.  Now, however, I cannot locate
> those notes.  Has the getchildren() method been deprecated, or will it
> be deprecated?

an element is a sequence, so there's no need to ever call getchildren
(if you need a real list, use list(elem)).

getchildren() will most likely be removed in some future version of
ElementTree.

</F>






More information about the Python-list mailing list