2

The equivalent of

static {
    // Code here
}

does not exist in Python by default. The closest thing, while being "Pythonic", seems to be to create a decorator and do the initialization there such as:

def static_initialization_decorator_for_myclass(cls):
    cls.initialize_static_stuff()
    return cls

@static_initialization_decorator_for_myclass
class MyClass:
    @classmethod
    def initialize_static_stuff():
        # Code here

Creating a new decorator for each class to do the same thing does not make sense. So, I thought of creating a standard decorator (say "initializestatic") and let that decorator call a given method name (say "init_static") like:

def initializestatic(cls):
    cls.init_static()
    return cls

@initializestatic
class MyClass:
    @classmethod
    def init_static():
        # Code here

This way, I can always use the same decorator and whenever I need a static initializer, I would put the following method in a class and would put @initializestatic decorator on top of the class:

@classmethod
def init_static():
    # Code here

Given it is that simple, why isn't there a built-in solution for static initialization in Python? I know that this sounds like a rant rather than a question but I am curious of the possible motives for excluding a static initializer from Python.

8

... why isn't there a built-in solution for static initialization in Python?

There is, you just put static initialization stuff in the class definition, i.e.

>>> class myclass:
...     print "anything in the class definition will be executed once!"
...     val = 42
...     def __init__(self,v):
...             self.v = v
...     def pr(self):
...             print self.v, self.val, myclass.val
...     def set(self,v):
...             self.val = v
...     def clsset(self, v):
...             myclass.val = v
...
anything in the class definition will be executed once!
>>> m = myclass(1)
>>> m.pr()
1 42 42
>>> m.set(2)
>>> m.clsset(24)
>>> m.pr()
1 2 24
>>> mm = myclass(2)
>>> mm.pr()
2 24 24
>>> mm.set(42)
>>> mm.pr()
2 42 24

Edit: I used print as an example of an arbitrary piece of code; here is another example:

>>> class myclass:
...     val = 42
...     dv = val * 1.2
...     def pr(self):
...             print self.dv
...
>>> m = myclass()
>>> m.pr()
50.4
  • But you cannot refer to anything in class. For example, you cannot do double_val = val * 2 or double_val = myclass.val * 2. These won't work. – Utku Oct 18 '18 at 1:59
  • 3
    @Utku Are you sure? Something like double_val = val * 2 should work. If not, consider asking on Stack Overflow. Note that the class body is executed like a function body and then a class is created with any local variables as class members, so you cannot reference myclass within the class definition. – amon Oct 18 '18 at 7:06
  • Some simple actions can be performed this way, but I don't see any means to call the classes' static methods – user508402 Apr 22 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.