[英]Python: Refer to objects dynamically
Apologies if this is a silly question. 抱歉,这是一个愚蠢的问题。
I have a list of potential dictionary keys here: 我在这里有一个潜在的字典键列表:
form_fields = ['sex',
'birth',
'location',
'politics']
I am currently manually adding values to these keys like so: 我目前正在手动向这些键添加值,如下所示:
self.participant.vars["sex"] = [Constants.fields_dict["sex"][0], Constants.fields_dict["sex"][1], self.player.name]
self.participant.vars["birth"] = [Constants.fields_dict["birth"][0], Constants.fields_dict["birth"][1],self.player.age]
self.participant.vars["location"] = [Constants.fields_dict["location"][0], Constants.fields_dict["location"][1],self.player.politics]
I'd like to be able to do a use a for loop to do this all at once like so: 我希望能够使用for循环来一次完成所有操作,如下所示:
for i in form_fields:
self.participant.vars[i] = [Constants.fields_dict[i][0], Constants.fields_dict[i][1], self.player.`i`]
Obviously, however, I can't reference the object self.player. 但是,显然,我无法引用对象self.player。
i
like that. i
喜欢那样。 Is there a way to reference that object dynamically? 有没有办法动态引用该对象?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.