[英]Printing value of a variable as a object name python
Here goes an newbie question on python. 这里是关于python的新手问题。
From the following function 从以下功能
def singlereader(url, linkGlue):
d = feedparser.parse(url)
tmp = []
for item in d.entries:
tmp.append(item.linkGlue) # line 5
return tmp
how would i use the variable value as the object name for "item" In line 5, i want to use the value of "linkGlue" variable. 我将如何使用变量值作为“ item”的对象名称在第5行中,我想使用“ linkGlue”变量的值。
If I understood you correctly, use: 如果我对您的理解正确,请使用:
getattr(item, linkGlue)
in place of 代替
item.linkGlue
I think the closest you can get here is to leverage operator.attrgetter or using the builtin getattr 我认为您可以在这里找到的最接近的是利用operator.attrgetter或使用内置的getattr
def singlereader(url, linkGlue):
from operator import attrgetter
d = feedparser.parse(url)
tmp = []
for item in d.entries:
#tmp.append(attrgetter(linkGlue)(item)) # line 5
tmp.append(getattr(item, linkGlue))
return tmp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.