0

I am trying to set default values while defining a class method and the default values are defined in a config function. Something like this:

from config import var1, var2

class A():

    def __init__(var_one=var1, var_two=var2):
        #do something

Now, is it a good idea to get the default values from a config file. I am being skeptical because I think the class definition shouldn't have any dependency on other files. Should I pass all the variables while using the class only?

glibdud
  • 7,550
  • 4
  • 27
  • 37
Biswadip Mandal
  • 534
  • 1
  • 4
  • 15

1 Answers1

0

The benefit of using a config file to save default values is that if the default values are being used by various classes across various python files, and you want to change default values, you only change them at one location, also you can always override default values and pass your own values to the class constructor, you are not having a dependency on another file

So I have a config.py as below

var1 = 5
var2 = 10

And my class is

class A():

    def __init__(var_one=var1, var_two=var2):
        self.var

Now I can either use default values to instantiate my class like so

a = A()
print(a.var_one)
#5
print(a.var_two)
#10

Or use my own values

a = A(15,20)
print(a.var_one)
#15
print(a.var_two)
#20

There is no right or wrong answer here, all dependent on the usecase at hand

Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40