Python中返回变量的UnboundLocalError

时间:2017-03-15 09:30:26

标签: python

我有以下Python 2.7代码(只是一个例子,因为实际代码要大得多)。

def test_function():
    ...
    x, y, z = parse_xml(filepath)
    test_dict[key].append([a, x, y, z, b])
    ...

def parse_xml(filepath):
    ...
    x = node.text  # Parse an XML file and set x to be the value of one of the nodes
    ...
    return x, y, z

脚本运行了几个小时才终止错误

File "test.py", line 660, in parse_xml 
  return x, y, z
UnboundLocalError: local variable 'x' referenced before assignment

我不确定导致错误的原因,因为我的x变量始终是局部变量。我需要在parse_xml之前放置test_function吗?

0 个答案:

没有答案