将连接的字符串作为全局变量返回
Returning concatenated string as global variable
所以这是我使用的代码:
global output_res
output_res = ""
def recurse(left, right, threshold, features, node, depth):
spacer = spacer_base * depth
if (threshold[node] != -2):
"""print(spacer + "if ( " + features[node] + " <= " + \
str(threshold[node]) + " ) {")"""
output_res += spacer + "if ( " + features[node] + " <= " + \
str(threshold[node]) + " ) {"
if left[node] != -1:
recurse (left, right, threshold, features, left[node], depth+1)
"""print(spacer + "}\n" + spacer +"else {")"""
output_res += spacer + "}\n" + spacer +"else {"
if right[node] != -1:
recurse (left, right, threshold, features, right[node], depth+1)
"""print(spacer + "}")"""
output_res += spacer + "}"
else:
target = value[node]
for i, v in zip(np.nonzero(target)[1], target[np.nonzero(target)]):
target_name = target_names[i]
target_count = int(v)
"""print(spacer + "return " + str(target_name) + " ( " + \
str(target_count) + " examples )")"""
output_res += spacer + "return " + str(target_name) + " ( " + \
str(target_count) + " examples )"
return output_res
recurse(left, right, threshold, features, 0, 0)
如您所见,recurse()
是一个递归函数,我的目标是检索 output_res
,并在主函数中使用它,使用这段代码我遇到了这个错误:
local variable 'output_res' referenced before assignment
更新
我找到了我正在寻找的确切解决方案:
temp_list = []
def recurse(temp_list, left, right, threshold, features, node, depth):
spacer = spacer_base * depth
if (threshold[node] != -2):
temp_list.append(spacer + "if ( " + features[node] + " <= " + \
str(threshold[node]) + " ) {")
if left[node] != -1:
recurse (temp_list, left, right, threshold, features, left[node], depth+1)
temp_list.append(spacer + "}\n" + spacer +"else {")
if right[node] != -1:
recurse (temp_list, left, right, threshold, features, right[node], depth+1)
temp_list.append(spacer + "}")
else:
target = value[node]
for i, v in zip(np.nonzero(target)[1], target[np.nonzero(target)]):
target_name = target_names[i]
target_count = int(v)
temp_list.append(spacer + "return " + str(target_name) + " ( " + \
str(target_count) + " examples )")
recurse(temp_list, left, right, threshold, features, 0, 0)
return '\n'.join(temp_list)
您需要将global
命令放入函数中,只需确保事先定义了全局变量:
output_res = ""
def recurse(left, right, threshold, features, node, depth):
global output_res
spacer = spacer_base * depth
if (threshold[node] != -2):
"""print(spacer + "if ( " + features[node] + " <= " + \
str(threshold[node]) + " ) {")"""
output_res += spacer + "if ( " + features[node] + " <= " + \
str(threshold[node]) + " ) {"
if left[node] != -1:
recurse (left, right, threshold, features, left[node], depth+1)
"""print(spacer + "}\n" + spacer +"else {")"""
output_res += spacer + "}\n" + spacer +"else {"
if right[node] != -1:
recurse (left, right, threshold, features, right[node], depth+1)
"""print(spacer + "}")"""
output_res += spacer + "}"
else:
target = value[node]
for i, v in zip(np.nonzero(target)[1], target[np.nonzero(target)]):
target_name = target_names[i]
target_count = int(v)
"""print(spacer + "return " + str(target_name) + " ( " + \
str(target_count) + " examples )")"""
output_res += spacer + "return " + str(target_name) + " ( " + \
str(target_count) + " examples )"
return
recurse(left, right, threshold, features, 0, 0)
您需要将全局语句放在函数内部,而不是外部
output_res = ""
def recurse(left, right, threshold, features, node, depth):
global output_res
...
如果你真的想使用全局变量
output_res = ""
def recurse(left, right, threshold, features, node, depth):
global output_res
//your code
//no need to do "return output_res"
recurse(left, right, threshold, features, 0, 0)
解释:
In [8]: myG = 5
In [9]: def fun1():
...: myG=45
...:
In [10]: def fun2():
....: print myG
In [11]: fun1()
In [12]: fun2()
5 //output
//Now change the fun1 with global
In [15]: def fun1():
....: global myG
....: myG=45
In [17]: fun1()
In [18]: fun2()
45 //output, this explains how global affects the scope
所以这是我使用的代码:
global output_res
output_res = ""
def recurse(left, right, threshold, features, node, depth):
spacer = spacer_base * depth
if (threshold[node] != -2):
"""print(spacer + "if ( " + features[node] + " <= " + \
str(threshold[node]) + " ) {")"""
output_res += spacer + "if ( " + features[node] + " <= " + \
str(threshold[node]) + " ) {"
if left[node] != -1:
recurse (left, right, threshold, features, left[node], depth+1)
"""print(spacer + "}\n" + spacer +"else {")"""
output_res += spacer + "}\n" + spacer +"else {"
if right[node] != -1:
recurse (left, right, threshold, features, right[node], depth+1)
"""print(spacer + "}")"""
output_res += spacer + "}"
else:
target = value[node]
for i, v in zip(np.nonzero(target)[1], target[np.nonzero(target)]):
target_name = target_names[i]
target_count = int(v)
"""print(spacer + "return " + str(target_name) + " ( " + \
str(target_count) + " examples )")"""
output_res += spacer + "return " + str(target_name) + " ( " + \
str(target_count) + " examples )"
return output_res
recurse(left, right, threshold, features, 0, 0)
如您所见,recurse()
是一个递归函数,我的目标是检索 output_res
,并在主函数中使用它,使用这段代码我遇到了这个错误:
local variable 'output_res' referenced before assignment
更新
我找到了我正在寻找的确切解决方案:
temp_list = []
def recurse(temp_list, left, right, threshold, features, node, depth):
spacer = spacer_base * depth
if (threshold[node] != -2):
temp_list.append(spacer + "if ( " + features[node] + " <= " + \
str(threshold[node]) + " ) {")
if left[node] != -1:
recurse (temp_list, left, right, threshold, features, left[node], depth+1)
temp_list.append(spacer + "}\n" + spacer +"else {")
if right[node] != -1:
recurse (temp_list, left, right, threshold, features, right[node], depth+1)
temp_list.append(spacer + "}")
else:
target = value[node]
for i, v in zip(np.nonzero(target)[1], target[np.nonzero(target)]):
target_name = target_names[i]
target_count = int(v)
temp_list.append(spacer + "return " + str(target_name) + " ( " + \
str(target_count) + " examples )")
recurse(temp_list, left, right, threshold, features, 0, 0)
return '\n'.join(temp_list)
您需要将global
命令放入函数中,只需确保事先定义了全局变量:
output_res = ""
def recurse(left, right, threshold, features, node, depth):
global output_res
spacer = spacer_base * depth
if (threshold[node] != -2):
"""print(spacer + "if ( " + features[node] + " <= " + \
str(threshold[node]) + " ) {")"""
output_res += spacer + "if ( " + features[node] + " <= " + \
str(threshold[node]) + " ) {"
if left[node] != -1:
recurse (left, right, threshold, features, left[node], depth+1)
"""print(spacer + "}\n" + spacer +"else {")"""
output_res += spacer + "}\n" + spacer +"else {"
if right[node] != -1:
recurse (left, right, threshold, features, right[node], depth+1)
"""print(spacer + "}")"""
output_res += spacer + "}"
else:
target = value[node]
for i, v in zip(np.nonzero(target)[1], target[np.nonzero(target)]):
target_name = target_names[i]
target_count = int(v)
"""print(spacer + "return " + str(target_name) + " ( " + \
str(target_count) + " examples )")"""
output_res += spacer + "return " + str(target_name) + " ( " + \
str(target_count) + " examples )"
return
recurse(left, right, threshold, features, 0, 0)
您需要将全局语句放在函数内部,而不是外部
output_res = ""
def recurse(left, right, threshold, features, node, depth):
global output_res
...
如果你真的想使用全局变量
output_res = ""
def recurse(left, right, threshold, features, node, depth):
global output_res
//your code
//no need to do "return output_res"
recurse(left, right, threshold, features, 0, 0)
解释:
In [8]: myG = 5
In [9]: def fun1():
...: myG=45
...:
In [10]: def fun2():
....: print myG
In [11]: fun1()
In [12]: fun2()
5 //output
//Now change the fun1 with global
In [15]: def fun1():
....: global myG
....: myG=45
In [17]: fun1()
In [18]: fun2()
45 //output, this explains how global affects the scope