-1

I usually get the url like this

response.url

I also get more date from the response.

I wonder if there is a way in which I can pass the things that I want and then use it in the response like this:

x = 'url'
return response.x
Nathaniel Mallet
  • 401
  • 2
  • 10
Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253

1 Answers1

2

You can use getattr:

x = "url"
return getattr(response, x)

From the docs:

getattr(object, name[, default])

Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example, getattr(x, 'foobar') is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.

Community
  • 1
  • 1
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • You should add the default argument, otherwise this will raise an exception. I forgot about the default third argument. – Burhan Khalid Jan 31 '14 at 13:59
  • 2
    It only raises an exception if the named attribute isn't there. In some cases, leaving it to raise an error is the correct behaviour, hence it is an optional argument. – jonrsharpe Jan 31 '14 at 14:01