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):
    return cls

class MyClass:
    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):
    return cls

class MyClass:
    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:

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.


1 Answer 1


... 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()
  • 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
    Commented Oct 18, 2018 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
    Commented Oct 18, 2018 at 7:06
  • 1
    Some simple actions can be performed this way, but I don't see any means to call the classes' static methods
    – user508402
    Commented Apr 22, 2019 at 20:09
  • @user508402 See stackoverflow.com/a/12718272/2075630. The static method objects contain a __func__ field containing the function itself. Arguably, it would be more intuitive if they were callable though.
    – kdb
    Commented Jan 12, 2021 at 10:10
  • @kdb: while the static function can be called that way, class initialization is still not possible since the class is not accessible. The method has as class attribute, but it points to 'staticmethod'.
    – user508402
    Commented Jan 23, 2021 at 14:24

Your Answer

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

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