I'm trying to build a python3 module for an HTTP RESTful API that I've coded.
My idea was to create a base class that should have a request.Session() attribute so I can assign an authorization token header to that and don't worry about it anymore and also a logger function and so on.
The problem is that a class called User inherits from 2 classes: PublicUser and base and I can't initialize them correctly.
It's the first time that I'm working with inherited class so obviously I'm missing something.
This is my folder structure:
examplemodule/
|--> __init__.py
|--> classes/
|--> base.py
|--> user.py
base.py
from requests import Session
from requests.sessions import session
class Logger:
def __init__(self):
pass
def log(self, message):
print(message)
class Base:
def __init__(self, token=None):
if not hasattr(self, 'logger'):
self.logger = Logger()
if not hasattr(self, 'session'):
self.session = Session()
self.session.headers.update(
{'authorization': 'Token {}'.format(token)}
)
# Try to login to see if token is valid, if not raise exception
# If token is valid then the retrieved user json is saved
self._user = {
'id': 1,
'username': 'test1',
'email': '[email protected]'
}
user.py
from .base import Base
PUBBLIC_USER_ATTRS = ['id', 'username']
PRIVATE_USER_ATTRS = ['email']
class PublicUser:
def __init__(self, user):
for k in PUBBLIC_USER_ATTRS:
setattr(self, k, user[k])
class User(Base, PublicUser):
def __init__(self, token=None):
super(Base, self).__init__(token=token)
super(PublicUser, self).__init__(self._user)
for k in PRIVATE_USER_ATTRS:
setattr(self, k, self._user[k])
__init__.py
from .classes.user import User
then to test my module I run:
import examplemodule
examplemodule.User(token='')
but unfortunately I get a TypeError at super(Base, self).__init__(token=token)
TypeError: super() takes no keyword arguments
What is the best way to get through this?