使用Python很容易声明类似的东西
self.x = "something"
print self.x #outputs "something"
我想要这样的东西:
param["key"] = "x"
self.param["key"] = "something" #here I actually want to access this "self" parameter as below with its value defined above
print self.x #supposed to output "something" as well. Note that "x" refers to value defined in the first line
有这样的事吗?有类似的替代方案吗?
提前致谢。
最佳答案
使用setattr
-- setattr(self, param['key'], 'something')
.
关于python - 如何在Python中定义泛型变量(语法问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2273211/