Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Conventional way of initializing class variables via methods (class methods or methods outside class?)

I have 3 different config files from which I would want to initialize different class variables for different major functions

Q1. What is the more conventional way of initializing class variables via methods (class methods or methods outside class)

Q2. How to initialize class variables via class methods

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

config.py

    class CONFIG:
        x_config = 'x_config.json'
        y_config = 'y_config.json'
        z_config = 'z_config.json'

>Solution :

Option 1

The most conventional way would be to initialize them in the init method of the class.

Option 2

You can initialize class variables in class methods by using the self keyword. For example:

class MyClass:
    def __init__(self):
        self.var1 = 1
        self.var2 = 2

    def class_method(self):
        self.var1 = 3
        self.var2 = 4
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading