-1

I have an object that needs to be initialised by reading a config file and environment variables. It has class methods, I want to make sure the object is initialised before the classmethod is executed.

Is there any way to initialise all classes of this sort of nature? I will probably have quite a few of these in my code. I'm coming from a Java/Spring background where simply putting @Service on top of the class or @PostConstruct over the initializer method would make sure it's called. If there's not a clean way to do this in normal Python, is there framework that'll make this easier?

So the class looks something like this

class MyClass(object):
    def setup(self):
        # read variables from the environment and config files

    @classmethod
    def my_method(cls, params):
        # run some code based on params and variables initialised during setup
Tersosauros
  • 883
  • 1
  • 12
  • 22
tt_Gantz
  • 2,786
  • 3
  • 23
  • 43
  • 1
    Are you *sure* you need those methods to be class methods then? Sounds like you have some state to manage here. – Martijn Pieters Apr 03 '16 at 09:00
  • It's a global state, like database endpoint and url for a particular environment the code is running in – tt_Gantz Apr 03 '16 at 09:01
  • Then make `setup` a class method too, call it always, and have that method check if a global has been set yet? – Martijn Pieters Apr 03 '16 at 09:02
  • Is there some reason you cannot have an `if __name__ == "__main__"` block which calls/does `setup()`? – Tersosauros Apr 03 '16 at 09:03
  • 1
    Still, I'd encapsulate that in a class I create one instance of. – Martijn Pieters Apr 03 '16 at 09:03
  • @MartijnPieters I was thinking that, is there a nicer way of doing it in Python so I don't have to keep putting `if not initialized: setup() ` – tt_Gantz Apr 03 '16 at 09:04
  • @tt_Gantz: yes, make this a proper class, and create the class when you first need it. That can either be at import time or via a utility function that returns the singleton instance (and creates it if it doesn't yet exist). That is, still, a *test if global exists* approach though. – Martijn Pieters Apr 03 '16 at 09:05
  • @Tersosauros I'm running a webserver and doing this in the not the class I use to run the webserver. I could add `MyClass1.setup(); MyClass2.setup()` etc. in the main class I run, but I'm looking for nicer way to have it defined in the class itself rather than always add it to the main class – tt_Gantz Apr 03 '16 at 09:06
  • @MartijnPieters so I either have to do it whenever I want to call the method, or inside the method. Is there a framework or decorators over the class that can help me keep the code a bit cleaner when doing this? – tt_Gantz Apr 03 '16 at 09:21
  • @tt_Gantz: I've always just written that myself. This is so simple, I doubt there is a library or framework for this. – Martijn Pieters Apr 03 '16 at 09:22

3 Answers3

1

You could always use a simple singleton implementation, which sounds like what you're trying to do. This post has examples of how to do it and some thoughts about whether you really need a singleton. Is there a simple, elegant way to define singletons?

Community
  • 1
  • 1
dirkdirk
  • 26
  • 1
0

Option #2.

class MyClass(object):
    # No setup()
    setting_A="foo"
    setting_B="bar"
    dict_of_settings={"foo":"bar"}
    # basically you can define some class variables here

    @classmethod
    def my_method(cls, params):
        print cls.setting_B # etc

This option avoids the use of any global code at all. However it comes at the price that your settings are no longer nicely encapsulated within an instance.

Tersosauros
  • 883
  • 1
  • 12
  • 22
-2

Yes.

class MyClass(object):
    def __init__(self): #instead of setup()
        # read variables from the environment and config files

    @classmethod
    def my_method(cls, params):
        # run some code based on params and variables initialised during setup
MyClass._instance=MyClass()

Basically, When you first import/load the file containing MyClass, it will run the __init__ constructor once for it's internal _instance. That instance (singleton) will then be accessible from all other class methods.

Tersosauros
  • 883
  • 1
  • 12
  • 22