【发布时间】:2018-07-17 23:58:55
【问题描述】:
import numpy as np
Method_name = "method_a"
#Method_name = "method_b"
def method_a (x, y):
result = x + y
return result
def method_b (x, y):
result = x * y
return result
result_method_a = np.zeros((0,1))
result_method_b = np.zeros((0,1))
x1 = 1
x2 = 5
for i in range (10):
result = method_a(x1, x2)
result +=1
print (result)
result_method_a = np.vstack((result_method_a, result))
x1 += 1
x2 += 5
如果我激活 method_a 或 method_b,有什么方法可以动态更改名称
例如: 如果我取消注释该行:
Method_name = "method_b"
然后我会得到:
result = method_b(x1, x2)
和:
result_method_b = np.vstack((result_method_b, result))
等等
这只是一个小例子。
【问题讨论】:
标签: arrays python-3.x numpy dynamic names