(Feb-05-2020, 01:15 PM)psolar Wrote: But when I import and instantiate the class, lists are empty.It does work as intended for me.
class Borg: shared_state = {} def __init__(self): self.__dict__ = self.shared_state self.state = 'Init'usage:
>>> from test11 import Borg >>> borg1 = Borg() >>> borg2 = Borg() >>> borg1.state 'Init' >>> borg2.state 'Init' >>> borg1.state = 'test' >>> borg1.state 'test' >>> borg2.state 'test'If done this way i would definitely make the variable name obvious that it is shared to avoid thinking it is an instance attribute independent from other instances.
I usually call Borg.shared_state directly when changing or referring to any class attribute. Im not sure which is more pythonic to be honest.
Recommended Tutorials: