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.
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()
50.4
-
But you cannot refer to anything in class. For example, you cannot do
double_val = val * 2
ordouble_val = myclass.val * 2
. These won't work.Utku– Utku2018年10月18日 01:59:20 +00:00Commented 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 referencemyclass
within the class definition.amon– amon2018年10月18日 07:06:33 +00:00Commented Oct 18, 2018 at 7:06 -
1Some simple actions can be performed this way, but I don't see any means to call the classes' static methodsresurrected user– resurrected user2019年04月22日 20:09:00 +00:00Commented 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– kdb2021年01月12日 10:10:56 +00:00Commented 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'.resurrected user– resurrected user2021年01月23日 14:24:15 +00:00Commented Jan 23, 2021 at 14:24
Explore related questions
See similar questions with these tags.
static { }
" in what language...?