How to work around a global variable that invokes some I/O operation?
If you have one single DataProvider
object in your application, you could simply remove the global variable and load the token in the __init__
method:
class DataProvider:
def __init__(self):
self.api_token = get_token_from_database()
Another possible lazy loading would be to initialize the global token to None
and then set it at first instanciation of a DataProvider object
API_TOKEN = None
class DataProvider:
def __init__(self):
global API_TOKEN
if API_TOKEN is None:
API_TOKEN = get_token_from_database()
self.api_token = API_TOKEN
Error processing still omitted for brievety...