1

I have 3 files: main, calc, and settings organized like this:

# main.py
import settings as s
import calc as c
class Main:
    def __init__(self):
        calc = c.Calc()
        self.my_var = user_input
        s.changing_variable = self.my_var

    def run(self):
        calc.do_something()     

# calc.py
import settings as s
class Calc:
    def __init__(self):
        self.test_variable = s.changing_variable

# settings.py
changing_variable = original_value

All code is run from the main file. I have an original value in the settings file that I now want to change based on the user input in my main file. This value should then be changed globally for all files.

The problem is that it is changed for the main file but not for the calc file. I assume this is because I import settings at the beginning of calc.py and therefore override the user input again.

Does anyone have any idea how to solve this without having to pass the user input variable around in the function calls?

Ibrahim Hasnat
  • 935
  • 1
  • 8
  • 16
Eli
  • 156
  • 8
  • Does this answer your question? [Function not changing global variable](https://stackoverflow.com/questions/12665994/function-not-changing-global-variable) – wakey Nov 19 '20 at 16:18
  • 2
    It would be cleaner to have the settings encapsulated in an object (maybe a data class for simplicity), then construct a settings object and fill it with whatever data, then manipulate the object as needed. Using an imported Python script as a config file is going to needlessly complicate things. – Carcigenicate Nov 19 '20 at 16:19
  • @Carcigenicate Making an object for config data makes a lot of sense in OP’s case since it seems he has a small number of data fields. But is this the approach you would use for an arbitrary number of data fields? (for context, I’m a noob and don’t know much about the alternatives, hence am trying to get a better idea by asking someone more experienced) – Asker Nov 20 '20 at 01:34
  • 1
    @Asker "Arbitrary number of fields" suggests that you should use a dynamic structure like a list or dictionary; depending on if your data is positional or if it's associated with keys. If you need some sort of permanence, turning a list/dict into JSON and writing that to file is common, as is using the `pickle` module to pickle structures. It depends a lot on how the data is structured/used, and if you need to maintain it between program runs. – Carcigenicate Nov 20 '20 at 01:38
  • 1
    @Asker If the amount of data that you have is large though, you may need a full database to efficiently store the data. An answer to that question depends on a lot of factors. – Carcigenicate Nov 20 '20 at 01:43

1 Answers1

0

First you need to initialize the variable once during the application life and make it in the global scope, so "settings.py" becomes:

# settings.py
def defing_global_variables():
    global changing_variable
    changing_variable = original_value

and invoke the fuction "defing_global_variables" in the main module to initialize the global variables. in "main.initi" function, set the value of "changing_variable" to user_input before instantiating the calc object, and make calc object in the class scope by using self.calc instead of calc, so you can invoke this object again in "main.run" function. Here is the all modifications for your code:

# main.py
import settings as s
import calc as c

# initialize golobal variables   # Added lines
s.defing_global_variables()  # Added lines


class Main:
    def __init__(self):
        self.my_var = 500 # user_input
        s.changing_variable = self.my_var
        self.calc = c.Calc()  # instinteat the object after stting the value, and make it in class scope by adding "self."

    def run(self):
        self.calc.do_something()  # adding self.


a = Main()
a.run()
c2 = c.Calc()
s.changing_variable = 123  # new value
c2.print_global_var()

# the results
# 500
# 123
# calc.py
import settings as s
class Calc:
    def __init__(self):
        self.test_variable = s.changing_variable

    def do_something(self):
        print(self.test_variable)

    def print_global_var(self):
        print(s.changing_variable)
# settings.py
def defing_global_variables():
    global changing_variable
    changing_variable = 0 # original_value

Good Luck :)

Nour-Allah Hussein
  • 1,439
  • 1
  • 8
  • 17