Reputation: 928
I am going to try to be clear. I have a ex.py
file I want to import in other py
files. In the ex.py
file there is a variable that I declare. However, I would like to declare that variable in the other py
files.
I have a file ex.py
:
variable = 5
whatever = 10 + variable
For example in an other .py
file, I would have:
from ex import whatever
variable = 10
print(whatever) # whatever object uses the variable
The result from printing whatever
will be 15
. I would like to have 20
as i declare variable
in the new script.
That is to streamline the code so that the same .py
file can be used in several other files.
Any contribution please.
Upvotes: 1
Views: 709
Reputation: 4207
If I understand you correctly, you want whatever
to always be variable + 5
, right? If you set whatever
in the ex.py
, its value is fixed to 15 and will not change.
In this case you should declare a function which calculates whatever
dynamically, like so:
def whatever(variable):
return variable + 5
If you want to have multiple .py files accessing the same global variable (the other interpretation of your question I can come up with), you might want to have a look at the singleton pattern
Upvotes: 2