1

I am using an API to call specific information from a website. I need to be able to parse through the list to utilize the functions. Example:

list = ['doThis','doThat']
for item in list:
    sampleobject.item

The issue is when I use this, I get an error saying "sampleobject has no attribute 'item'".

Is there a way that I can pull the quote out of the string to do this?

dhinchliff
  • 1,106
  • 8
  • 17

2 Answers2

1

Try:

methods = ['doThis','doThat']
for method_name in methods:
    method = getattr(sampleobject, method_name)
    method()

Though it would be easier to do:

sampleobject.doThis()
sampleobject.doThat()
dhinchliff
  • 1,106
  • 8
  • 17
0

You can call getattr(sampleobject, item) to get the content of a property with the name equal to what is stored in item, which is an element from your list. I think the problem is not about quotes at all. The problem is that syntax object.member means: evaluate a property named member that is stored in a variable named object. And you expect it to mean: evaluated a property with the name stored in member.

Dmytro Y.
  • 86
  • 1
  • 6