0

I have the following function that takes in a response, a CallableInterface object, and an attribute as a String.

def assert_response(response, api, expectedResponse):
    # api = self.Startup
    # expectedResponse = "ERROR_NOT_SUPPORTED"
    eval(api + "." + expectedResponse) 

The line seen above should evaluate to an attribute call as such:

self.Startup.ERROR_NOT_SUPPORTED

However, I am getting the following error because the value contained in expectedResponse is not being expanded.

AttributeError: 'CallableInterface' object has no attribute 'expectedResponse'

Any suggestions on how to work around this?

2
  • 1
    I have read twice. I still can't figure out what you are asking. Maybe you should paste more code? Commented Jun 23, 2015 at 13:54
  • Reworded some parts for clarity. Sorry. Commented Jun 23, 2015 at 13:58

1 Answer 1

1

You should use getattr to access object attributes:

getattr(api, expectedResponse)

If the api is an object (i.e. self.Startup) which attribute is named as the value of expectedResponse, that should do it.

But before anything, make sure that contents of expectedResponse is actually what you think it is, by printing it beforehand.

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

1 Comment

I like this much better than OP's eval approach, but I suspect he'll get the same "has no attribute" error.

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.