__init__ and instance show different values for instance attributes
Question: I am attempting to create a subclass of `Response` from the "Requests" library
for Python. When I execute the following with Python 2.7.6 and Requests 2.5.3:

    
    
    import requests
    
    class Page(requests.Response):
        # A Page is a Response that reported itself to be OK (HTTP "200").
    
        def __init__(self, url):
            requests.Response.__init__(self)
            self = requests.get(url)
            assert self.status_code is 200
            print "Status code according to __init__: " + str(self.status_code)
    
    p = Page("http://www.google.com/")
    print "Status code according to instance: " + str(p.status_code)
    

I get this output:

    
    
    Status code according to __init__: 200
    Status code according to instance: None
    

This is surprising to me. I thought those two calls were addressing the same
attribute of the same instance, and had therefore expected them to give the
same output.

Indeed, all of the instance's attributes show themselves to be populated when
called from `__init__` (i.e. not just `self.status_code` but also `self.text`,
etc). Yet, if I call the same attributes from the instance (e.g. `p.text`),
they yield `None` or suchlike, as though they had not been initialized.

Why this disparity between what `__init__` sees and what the instance sees?


Answer: Evidently those two calls were **not** addressing the same attribute of the
same instance. That is why they did not give the same output. This solves the
problem, by creating a Request object and then copying its attributes to the
Page object:

    
    
    import requests
    
    class Page(requests.Response):
        # A Page is a Response that has been retrieved and found OK (HTTP "200").
    
        def __init__(self, url):
            requests.Response.__init__(self)
            r = requests.get(url)
            self.__dict__.update(r.__dict__)
            assert self.status_code is 200
            print "Status code according to __init__: " + str(self.status_code)
    
    p = Page("http://www.google.com/")
    print "Status code according to object: " + str(p.status_code)
    

I suspect there's a more elegant solution available (although I haven't come
up with one yet), so I'll not mark this question as "answered" for now. Please
submit a better solution if you have one!

