Edit: this is unfortunately not answered in What is the difference between __init__ and __call__ in Python?
class OAuth2Bearer(requests.auth.AuthBase):
def __init__(self, api_key, access_token):
self._api_key = api_key
self._access_token = access_token
def __call__(self, r):
r.headers['Api-Key'] = self._api_key
r.headers['Authorization'] = "Bearer {}".format(self._access_token)
return r
#############
class AllegroAuthHandler(object):
def apply_auth(self):
return OAuth2Bearer(self._api_key, self.access_token) # what will happen here?
I read about __init__
and __call__
, but I still don't undestand what is going on in this code
I don't understand:
1.) Which method will be called, __init__
or __call__
2.) If __init__
, then __init__
doesn't return anything
3.) If __call__
, then __call__
can't be called with two parameters
I think __init__
should be called, because we have X()
, not x()
from example below as in this answer:
x = X() # __init__ (constructor)
x() # __call__