Q:

Pre-filling request headers using classes derived from requests.Session in Python

I'm trying to refactor some code in which many HTTP requests are made using the requests module. Many of these requests have (partially) the same headers, so I would like to 'pre-fill' these using Session objects.
However, I'm having difficulty making multiple inheritance work in this context. Here is what I've tried:
import requests, time

requestbin_URL = 'http://requestb.in/1nsaz9y1'      # For testing only; remains usable for 48 hours
auth_token = 'asdlfjkwoieur182932385'               # Fake authorization token

class AuthorizedSession(requests.Session):
    def __init__(self, auth_token):
        super(AuthorizedSession, self).__init__()
        self.auth_token = auth_token
        self.headers.update({'Authorization': 'token=' + self.auth_token})

class JSONSession(requests.Session):
    def __init__(self):
        super(JSONSession, self).__init__()
        self.headers.update({'content-type': 'application/json'})

class AuthorizedJSONSession(AuthorizedSession, JSONSession):
    def __init__(self, auth_token):
        AuthorizedSession.__init__(self, auth_token=auth_token)
        JSONSession.__init__(self)

""" These two commented-out requests work as expected """
# with JSONSession() as s:
#     response = s.post(requestbin_URL, data={"ts" : time.time()})

# with AuthorizedSession(auth_token=auth_token) as s:
#     response = s.post(requestbin_URL, data={"key1" : "value1"})

""" This one doesn't """
with AuthorizedJSONSession(auth_token=auth_token) as s:
    response = s.post(requestbin_URL, data={"tag" : "some_tag_name"})

If I inspect the result of the last request at http://requestb.in/1nsaz9y1?inspect, I see the following:

It seems like the Content-Type field is correctly set to application/json; however, I don't see an Authorization header with the fake authentication token. How can I combine the AuthorizedSession and JSONSession classes to see both?

A:

I've found that the request works if I define AuthorizedJSONSession more simply as follows:
class AuthorizedJSONSession(AuthorizedSession, JSONSession):
    def __init__(self, auth_token):
        super(AuthorizedJSONSession, self).__init__(auth_token=auth_token)

The resulting request now has updated both the Authorization and Content-Type headers:

I've understood that when a class inherits from multiple classes which in turn inherit from the same base class, then Python is 'smart enough' to simply use super to initialize.

