开发者

how to define generic variables in Python (syntax question)

开发者 https://www.devze.com 2022-12-20 02:46 出处:网络
With Python it is easy to declare something like self.x = \"something\" print self.x #outputs \"something\"

With Python it is easy to declare something like

self.x = "something"
print self.x #outputs "something"

I want to have something like this:

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 th开发者_Go百科at "x" refers to value defined in the first line

Is there any such thing? Is there any similar alternatives?

Thanks in advance.


Use setattr -- setattr(self, param['key'], 'something').

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号