I have a python function:
class MyClass:
my_class_variable: str = Optional[None]
@classmethod
def initialize(cls):
cls.my_class_variable = cls.some_function()
I plan to use it like:
x = MyClass.my_class_variable
How can I guarantee have my_class_variable to have initialized with a value, eg how can I force call initialize() ?
my_class_variableto be a class variable,__init__only works with instancesMyClass.initialize()after your class definition.__init__. In any case, as I already pointed out, the simplest way to guaratneeinitializehas been called is to call it. So after the class definition forclass MyClass, just writeMyClass.initialize()after.