0

I have a dataclass that contains some attributes like the example below.

How can I create a static instance of the class and keep it as part of the same class?

The reason I need this is because later I would want to access these fields by doing for example TestClass.STATIC_INSTANCE1.attribute1.

@dataclass
class TestClass:
    attribute1: str
    attribute2: int
    
    STATIC_INSTANCE1 = TestClass("instance1", 1)
    STATIC_INSTANCE2 = TestClass("instance2", 1)
2
  • 3
    You can't. Until the class definition completes you can't instantiate it. You'd have to do it afterwards. Commented Jul 19, 2022 at 13:22
  • 2
    After your class definition, put TestClass.STATIC_INSTANCE1 = TestClass("instance1", 1) Commented Jul 19, 2022 at 13:22

2 Answers 2

2

Usually doing that is frowned upon, as it creates circular references, problematic especially when you generate the class dynamically inside functions.

If that is not your case, you can create it like so:

@dataclass
class TestClass:
    attribute1: str
    attribute2: int


TestClass.STATIC_INSTANCE1 = TestClass("instance1", 1)
TestClass.STATIC_INSTANCE2 = TestClass("instance2", 1)

I suggest you to avoid doing it altogether, and just defining STATIC_INSTANCE1 outside of the class, but it will require additional from...imports on the user side.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

Doesn't the "singleton" Design Pattern usually describes a way to design a class in order to have a "single" instance? I agree that it closely resembles this case, but the finality/purpose of this design pattern seem to me to be more than what is needed here. Only nitpicking here, but wouldn't it be better to formulate it as "inspired from the singleton design pattern implementation"? Or have I misunderstood the purpose of the singleton pattern?
@GregoirePelegrin you are quite correct, it is not the singleton pattern, as the class can be instantiated more than once. It is close however, if you expect to only use the static instances.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.