I am developing a simple application which hava a file Constants.py containing all configuration, it is like this
x = y
during execution of program , the value of y changes , I want value of x o get updated too , automatically, this can be reffered as binding, how can I achieve this
In Python variable names point at values.
x=ytells Python that the variable namexshould point at the value thatyis currently pointing at.When you change
y, then the variable nameypoints at a new value, while the variable namexstill points at the old value.You can not achieve what you want with plain variable names.
I like KennyTM’s suggestion to define
xas a function since it makes explicit that the value ofxrequires running some code (the lookup of the value of y).However, if you want to maintain a uniform syntax (making all the constants accessible in the same way), then you could use a class with properties (attributes which call getter and setter functions):
Constants.py:
Your script.py:
Here you change the “constant” y:
And the “constant” x is changed too:
You can change
xalso,and
ytoo gets changed: