Two ElementTree questions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mirandacascade@yahoo.com

    #1

    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.getchildr en() == 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?

  • Fredrik Lundh

    #2
    Re: Two ElementTree questions

    mirandacascade@ yahoo.com wrote:
    [color=blue]
    > 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.getchildr en() == 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?[/color]

    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).
    [color=blue]
    > 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?[/color]

    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>



    Comment

    Working...