Not working in Python 2, Python 2 vs Python 3 Inheriting a wrapper
Question: I am working on a Flask based OAuth project that has a single instance of
`OAuthSignIn` class. This class has three subclasses for each OAuth client I
support, Facebook, Twitter, and Google. There is only one instance of each on
the server and they all contain an object called `oauth_session` used to make
endpoint calls to the appropriate OAuth endpoint. To ensure these calls are on
the behalf of the current user, before I make a call, I have to edit the
`access_token` variable of the `oauth_session` object to the current user's
`session['access_token']`. I want to do this via a wrapper found in the super
class (OAuthObject).

The following is a very basic model of what I am doing on the server just show
the inheritance of it and what not. I made a wrapper to do edit the subclass's
`oauth_session.access_token` and threw that in the super class of `OAuthObect`
so I only have to write it once.

    
    
    from functools import wraps
    
    class OAuthSessionObject():
      def __init__(self):
        self.access_token = "r424jt3g"
    
    class OAuthObject():
      def __init__(self):
        pass
    
      def auth_request(f):
        @wraps(f)
        def wrapper(self):
          self.oauth_session.access_token = 444
          #self.oauth_session.access_token = session['access_token']
          return f(self)
        return wrapper
    
    
    class GoogleObject(OAuthObject):
      def __init__(self):
        self.oauth_session = OAuthSessionObject()
    
      @OAuthObject.auth_request
      def make_request(self):
        print("Making a request with updated access_token:", self.oauth_session.access_token)
    
    google = GoogleObject()
    google.make_request()
    

### Python 2 vs Python 3

Problem with this, it only [works](https://repl.it/Bcal) in Python 3, and I am
currently running Python 2 server on heroku and locally, I get the following
errors:

    
    
    @OAuthSignIn.auth_request
    TypeError: unbound method auth_request() must be called with OAuthSignIn instance as first argument (got function instance instead)
    

How can I make this work in Python 2??


Answer: ### Figured out the solution but need explanation:

All I had to do to get it to work in Python 2 was to modify the wrapper
function to look like this:

    
    
    @classmethod
    def auth_request(self, f):
        @wraps(f)
        def wrapper(self):
            self.oauth_session.access_token = session['access_token']
            return f(self)
        return wrapper
    

Had to make it a `@classmethod` and just add `self` as the first parameter to
the `auth_request` declaration. Not sure why though, if someone would like to
explain why this is treated differently between the two versions of Python
that would be great.

