[Tutor] returning from a function?

Suzanne Little s349929@student.uq.edu.au
Fri, 16 Mar 2001 15:03:30 +1000 (GMT+1000)


Hi,

It was difficult to think of a subject line for this because I don't know
what's going on. I have a function, listed below, but it returns None
instead of what I think it should - which is a unicode string. The
function is intended to be part of a class which will search an xml file
for text from the location specified and I want it to return either a list
of nodes or the value of the node. I've tried returning various things,
strings, node objects, but everything is None.

Some code and a sample run follows
------------------------
from string import *
from xml.dom.minidom import parse
import xml.dom

def accessData(baseNode, accessList):  #recursive function
    if len(accessList) == 0:
        print baseNode  #for debug
        print baseNode.firstChild  #for debug
        res = baseNode.firstChild.data
        print res, type(res)  #for debug
        return res
    else:
	#Recursion code <snipped>


if __name__ == '__main__':
    f = open('some xml file', 'r')
    dom = parse(f)
    path = 'Top.NextNode.SomeOtherNode.TheNodeIWant'
    al = split(path, '.')
    al = al[1:]
    print accessData(dom.documentElement, al)
------
When I run this I get the following:
$python2.0 accessData.py
<DOM Element: Text at 1890636>
<DOM Text Node "Problem 1">
Problem 1 <type 'unicode'>   #This is the value that I want returned
None  #This is what I'm getting returned

I've also tried importing the code into an interpretter session and
calling the function but I get the same results.

I'm using python2.0 on solarius. Any suggestions?

Thanks,
Suzanne

--------------------------------------------------------------------------
"Contrariwise," continued Tweedledee, "If it was so, it might be; and if
it were so, it would be; but as it isn't, it ain't.  That's logic"
                             -Lewis Carroll
--------------------------------------------------------------------------