I have an application where I'd like to make the first instance of a class available for access by all subsequent instances of that class. I implemented this like:
class Basic(object):
def __init__(self, initial=False):
super(Basic, self).__init__()
if initial:
Basic.initial = self
It works nicely, but when I try to serialize it with pickle
or dill
, the dynamically created class attribute Basic.initial
does not serialize properly (How to write all class variables to disk with dill?).
I've been told that what I'm doing is "bad" and I can understand that because it does seem kinda roundabout.
I want that first instance to be available for use in all future instances without passing it around constantly.
Is there a better way to accomplish this? Maybe one that will serialize properly?