elementtree behavior

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Arnold

    #1

    elementtree behavior

    I have an XML snippet that I parse with ElementTree, and I get an element
    called self.makeToc

    Now this code:

    print self.makeToc
    if self.makeToc != None:
    print 'here'
    if self.makeToc: print 'but not here'

    gives this result:
    <Element toc at 40197e88>
    here

    Clearly self.makeToc has a value, so why isn't "if self.makeToc" True?
    That is, if self.makeToc isn't None, why don't I get past 'if self.makeToc'

    Can someone point out what am I not understanding?

    thanks,
    --Tim



  • Paul McGuire

    #2
    Re: elementtree behavior

    "Tim Arnold" <tiarno@sas.com > wrote in message
    news:cj9j8m$jn4 $1@license1.unx .sas.com...[color=blue]
    > I have an XML snippet that I parse with ElementTree, and I get an element
    > called self.makeToc
    >
    > Now this code:
    >
    > print self.makeToc
    > if self.makeToc != None:
    > print 'here'
    > if self.makeToc: print 'but not here'
    >
    > gives this result:
    > <Element toc at 40197e88>
    > here
    >
    > Clearly self.makeToc has a value, so why isn't "if self.makeToc" True?
    > That is, if self.makeToc isn't None, why don't I get past 'if[/color]
    self.makeToc'[color=blue]
    >
    > Can someone point out what am I not understanding?
    >
    > thanks,
    > --Tim
    >[/color]
    Not sure about your XML Element classes, but a number of Python objects will
    evaluate to false even if not set to None:
    - boolean with value of 'false'
    - integer with value of 0
    - list with no elements

    Perhaps your Element class evaluates to false if it has no child nodes, or
    some similar thing.

    -- Paul


    Comment

    • Fredrik Lundh

      #3
      Re: elementtree behavior

      Tim Arnold wrote:
      [color=blue]
      >I have an XML snippet that I parse with ElementTree, and I get an element
      > called self.makeToc
      >
      > Now this code:
      >
      > print self.makeToc
      > if self.makeToc != None:
      > print 'here'
      > if self.makeToc: print 'but not here'
      >
      > gives this result:
      > <Element toc at 40197e88>
      > here
      >
      > Clearly self.makeToc has a value, so why isn't "if self.makeToc" True?
      > That is, if self.makeToc isn't None, why don't I get past 'if self.makeToc'[/color]

      because it's a sequence without any items.

      this is discussed in the element overview:



      Note that in ElementTree 1.2 and earlier, the sequence behaviour
      means that an element without subelements tests as false (since it's
      an empty sequence). To check the return value from a function or
      method that may return None instead of a node, you must use an
      explicit test.

      node = fetchnode()

      if not node: # careful!
      print "node not found, or node has no subnodes"

      if node is None:
      print "node not found"

      </F>



      Comment

      Working...