Is there such a thing as a static constructor in Python?
How do I implement a static constructor in Python?
Here is my code… The __init__ doesn’t fire when I call App like this. The __init__ is not a static constructor or static initializer.
App.EmailQueue.DoSomething()
I have to call it like this, which instantiates the App class every time:
App().EmailQueue.DoSomething()
Here is my class:
class App:
def __init__(self):
self._mailQueue = EmailQueue()
@property
def EmailQueue(self):
return self._mailQueue
The problem with calling __init__ every time is that the App object gets recreated. My “real” App class is quite long.
Hint: anything that references
selfis going to require an instantiation of the class. You could do it like this:But come on, that seems like a lot of fluff. I’m with SLaks, just initialize it outside of the class. Alternatively, you could look into the singleton pattern.