I've written the following base static class in python:
from abc import ABC,abstractmethod
import typing
from main_module import utils
class BaseCLS(ABC):
credentials = None # <--- I want to set this var
def __init_session(caller) -> None:
credentials = utils.get_creds() # <--- not the right var
@staticmethod
@__init_session
def process(path: str) -> None:
raise NotImplementedError
The base class private method __init_session
is triggered by a decorator each time process
is called. I'm trying to set the credentials
class var from within the private method with no luck. How can I achieve my goal?
If you want to set the class variable in derived classes, the following would work.
from abc import ABC,abstractmethod
import typing
import random
class BaseCLS(ABC):
credentials = None # <--- I want to set this var
def __init_session(caller) -> None:
def inner(cls, path):
# credentials = utils.get_creds()
cls.credentials = random.random()
caller(cls, path)
return inner
@classmethod # <--- changed to classmethod
@__init_session
def process(cls, path: str) -> None:
pass
class B(BaseCLS):
pass
class C(BaseCLS):
pass
B.process("a")
C.process("b")
print(B.credentials)
print(C.credentials)