470,636 Members | 1,471 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,636 developers. It's quick & easy.

Two ElementTree questions

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? (I don't have a definition for 'preferable'; I'm hoping the
reply to this question will suggest why a different method may be
preferable.)

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?

Apr 28 '06 #1
1 951
mi************@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>

Apr 28 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Stewart Midwinter | last post: by
1 post views Thread by Greg Wilson | last post: by
3 posts views Thread by Matthew Thorley | last post: by
1 post views Thread by mirandacascade | last post: by
1 post views Thread by mirandacascade | last post: by
15 posts views Thread by Steven Bethard | last post: by
7 posts views Thread by mirandacascade | last post: by
reply views Thread by Greg Aumann | last post: by
2 posts views Thread by mirandacascade | last post: by
???
reply views Thread by Stoney L | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.