0

My code:

r = requests.get('http://www.pythonchallenge.com/pc/def/banner.p')
t = urlopen('http://www.pythonchallenge.com/pc/def/banner.p')

print(r)

'Response [200]'

print(t)

'http.client.HTTPResponse object at 0x0430A370'

Why does requests.get only return the object instance (in this case the respond code) while urlopen returns the actual object?

My question then would be: how can I use requests to return an object instead of the response code? (I want to desearialize the content using pickle)

Jeff Learman
  • 2,914
  • 1
  • 22
  • 31
Dan Kap
  • 1
  • 1
  • What an object does with a `print(x)` call doesn't completely encompass what has actually been "returned " to `x`. – n1c9 Jun 08 '18 at 16:53
  • 1
    You're a bit confused about what 'object instance' means. It is the same thing as 'actual object'. What you meant to say is that it appears to return the response code rather than the response object. However, that's just due to the way the response class's string conversion function works. Note that the requests response object is a different class than the http.client.HHTResponse object returned by urlopen. They both represent a response, but using different object classes. – Jeff Learman Jun 08 '18 at 16:59
  • To get a better idea of what is being returned (print-debugging python), use `from pprint import pprint` and `pprint(r)` – Jeff Learman Jun 08 '18 at 17:02
  • 1
    @JeffLearman `pprint` is a great suggestion, though in this particular case it will just return the response code again. It would be more helpful to do `pprint(vars(r))` – HFBrowning Jun 08 '18 at 17:06

1 Answers1

4

You are confusing what is returned by print with the object itself. requests.get does get the object. The developer of requests made the executive decision to return r.status_code when you call the print function. They could have returned anything: r.text, or r.raw, for example. It sounds like you were expecting to see the latter.

If you're interested, here is a bit more information about how developers can define what print returns: How to print a class or objects of class using print()?

HFBrowning
  • 2,196
  • 3
  • 23
  • 42