2

Here goes an newbie question on 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.

2 Answers 2

2

If I understood you correctly, use:

getattr(item, linkGlue)

in place of

item.linkGlue
Sign up to request clarification or add additional context in comments.

Comments

1

I think the closest you can get here is to leverage operator.attrgetter or using the builtin 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.