这是一个 Python 错误吗?递归函数中的 for 循环后变量丢失值。这是测试代码。我真的在解析 XML。
def findversion(aNode, aList, aFlag):
print "FindVersion ", aNode[0:1]
print "Findversion ", aFlag
if aNode[1].find('Software') != -1:
aFlag = 1
aList.append(aNode[1])
if aFlag == 1 and aNode[0] == 'b':
aList.append(aNode[1])
print "Before for ", aFlag
for elem in aNode[2:]:
print "After for ", aFlag
findversion(elem,aList,aFlag)
node = ['td', 'Software version']
node2 = ['b', '1.2.3.4' ]
node3 = [ 'td', ' ', node2 ]
node4 = [ 'tr', ' ', node, node3 ]
print node4
myList = list()
myInt = 0
findversion(node4,myList,myInt)
print "Main ",myList
在下面的程序输出中,我总是希望输出之前的输出与输出之后的相同。
程序输出:
['tr', ' ', ['td', 'Software version'], ['td', ' ', ['b', '1.2.3.4']]]
FindVersion ['tr']
Findversion 0
Before for 0
After for 0
FindVersion ['td']
Findversion 0
Before for 1
After for 0
FindVersion ['td']
Findversion 0
Before for 0
After for 0
FindVersion ['b']
Findversion 0
Before for 0
Main ['Software version']
蟒蛇版本:
Python 2.7.3 (default, Dec 18 2012, 13:50:09)
[GCC 4.5.3] on cygwin
Type "help", "copyright", "credits" or "license" for more information.