This is known as the singleton design pattern. It's most widely-used in Java, and as far as I know the textbook implementation is to use a method to initialize the static instance when necessary and no sooner, and to obtain that static instance:
@dataclass
class TestClass:
attribute1: str
attribute2: int
_STATIC_INSTANCE1 = None
_STATIC_INSTANCE2 = None
@staticmethod
def get_static_instance1():
if TestClass._STATIC_INSTANCE1 is None:
TestClass._STATIC_INSTANCE1 = TestClass("instance1", 1)
return TestClass._STATIC_INSTANCE1
# ditto for static instance 2
However, since, unlike Java, python allows free-floating objects (not everything needs to be a class), please consider whether these static instances actually need to be bound to the TestClass itself, or whether they can float in the same file:
@dataclass
class TestClass:
attribute1: str
attribute2: int
...
pass
STATIC_INSTANCE1 = TestClass("instance1", 1)
STATIC_INSTANCE2 = TestClass("instance2", 1)
# import STATIC_INSTANCE1 and STATIC_INSTANCE2 from the same file as TestClass,
# but separately
This latter approach is preferred in Python when possible.
TestClass.STATIC_INSTANCE1 = TestClass("instance1", 1)