Calling constructor of inherited class with only static methods

Viewed 1000

I have two Python classes. One inherits from the other. Both contain only static methods. The inherited class has class variables that need to be initialized from an external config file. I was trying to come up with a way to get the inherited class's constructor called without creating an instance.

The code below is a lean example of what I'm doing. The code works. I'm not sure, however, if this is just bad practice. I have no reason NOT to instantiate the class before use. I just wanted to develop a solution that did not require an instance to do what I need it to since the class is just static methods. It IS required that the inherited class's constructor is called.

Also, can someone tell me what the happens to the NEW class instance I create in the method 'foo'?

import json

class Cloud(object):
    val = None  # ....................................... Class Variable

    def __init__(self):  # .............................. Class Constructor
        with open('config.dat', 'r') as f:
            dat = json.load(f)
        Cloud.val = dat['val']  # ....................... overwrite Class Variable
        print('Class Cloud -- constructor called.')

    @staticmethod
    def foo():  # ....................................... static method, does not require instance
        Cloud.__init__(Cloud.__new__(Cloud))  # ......... call the constructor on a NEW instance
        print('Class Cloud, class variable (val):={}'.format(Cloud.val))

class Sensor(Cloud):

    @staticmethod
    def bar():
        print('bar')

# run Static Method of Inherited Class Cloud
# call Class Cloud constructor
Sensor.foo()

EDIT:

The class Cloud is used to MQTT data to a cloud server. Many other classes inherit from this so data transmissions are easily handled. The class Sensor is used to collect data from various sensors. At no point are any instances of these classes required because the data just gets passed along. The reason I use classes is to group certain methods together, again not required. The only thing I need is to run the constructor of Cloud so that the class variables get updated. I could handle this inside the staticmethod, I just didn't.

Again, just looking for feedback. Good idea, bad idea...

1 Answers

Since it's effectively a static class variable, couldn't you just initialize it in the class definition?
I talking about something like this:

import json

class Cloud(object):
    with open('config.dat', 'r') as f:
        dat = json.load(f)
        val = dat['val']
        del dat  # Keep this variable from becoming a class attribute.

    def __init__(self):  # .............................. Class Constructor
#        with open('config.dat', 'r') as f:
#            dat = json.load(f)
#        Cloud.val = dat['val']  # ....................... overwrite Class Variable
        print('Class Cloud -- constructor called.')

    @staticmethod
    def foo():  # ....................................... static method, does not require instance
#        Cloud.__init__(Cloud.__new__(Cloud))  # ......... call the constructor on a NEW instance
        print('Class Cloud, class variable (val): {}'.format(Cloud.val))

class Sensor(Cloud):

    @staticmethod
    def bar():
        print('bar')

# run Static Method of Inherited Class Cloud
# call Class Cloud constructor
Sensor.foo()

This way you wouldn't need to construct a throw-away instance of Cloud to get the variable initialized. Besides, it seems inefficient to be reading the (whole) file in everytime an instance is created.

Related