I want to create a python program where I can modify the values in a program and use the modified values in another program. Here's an example:
vars.py
a = 20
b = 30
x = 20+30
func.py
import vars
def changevalues():
vars.a = 40
vars.b = 50
main.py
import vars
import func
func.changevalues()
print(vars.x)
When I run main.py, the output I want is 90, but it adds up 20 and 30 and the output is 50.
vars.xwhich you set to20+30invars.py. It doesn't matter what you setaandbto whenxis still set to 50. Also worth mentioning that you are simply calling print onvars.xso whatever that is set to is what's going to print. The function isn't affecting anything.x = a + b.xis set when the file is imported. So later changes toaand/orbwould not change the value ofxin that case either.xwill also be changed along withaandb?xas anint, which is immutable. So the only way to change it is by assigning it to another value. If you want it to track changes inaandb, thenxneeds to be a callable (a function, for example), a class property, or a mutable type, like a list, for example.