StarBucK
StarBucK

Reputation: 219

Sharing variable between files in python: why are variable modified from whenever file is using them?

[edit] I renamed testons_constants to testons_variables to make more clear that I want to be able to modify the variables. If in the comment testons_constants appear take in account that it corresponds to the new name testons_variables

I am trying to understand how the variables are shared between files in python.

I made for this purpose three files:

testons.py:

import testons_variables
import testons_functions

# The goal of this file is to understand how python deals with variables shared between files 



print(testons_variables.A) # We can access the variable from the other file
testons_variables.A=5 # We can modify its value
print(testons_variables.A)
testons_functions.fct()
print(testons_variables.A) # The value has been modified from testons_functions

testons_variables.py:

A=0

testons_functions.py

import testons_variables

def fct():
    print(testons_variables.A) # this print will show the value of the variable from testons before the call of the function, and not from
    # testons_variables
    testons_variables.A=50 # We can modify the value
    print(testons_variables.A) 

Here are the outputs when I run testons.py:

0
5
5
50
50

Now, doing "reverse understanding", I realize that wherever the variable testons_variables.A is being modified, it will be modified for all files using it. Indeed, if I modify it in the file testons.py, it will be also modified inside testons_functions.py. And if I modify it in testons_functions.py, it will also be modified for testons.py.

This is my understanding now. So it seems that variables shared between files are modified for everyone, wherever the modification has been done.

What confuses me is that if we use global variable, to modify them within a function we need the keyword "global" to allow the global modification. I never used such keyword here. I know it is not exactly the same situation (I am not using a global variable inside function within a unique file), but I am anyway disturbed by this behavior which makes me think that I am probably missing a point here.

I am then wondering if I understood properly what happens when variable are being shared between files, and this is my question.

Upvotes: 2

Views: 86

Answers (2)

Mark Ransom
Mark Ransom

Reputation: 308520

Modules are objects too. When you define a variable in a module outside the scope of a function, it belongs to the module. It is not global.

When you import a module, you get back the same module object that anyone else who imports the object gets. Python caches modules so that no matter how many times you import it or how many places, you always get only one object containing that module. Because the module is shared, all the variables in that module are likewise shared.

Upvotes: 0

Vishnudev Krishnadas
Vishnudev Krishnadas

Reputation: 10970

Just print reference of imported teston_variables in both teston.py and teston_functions.py

print(id(testons_variables))

You will notice that both print statements give the same value, so, although they are imported in different places, they use the same reference.

This means that change in one will affect others, as they are pointing to the same memory location.

Now, regarding the usage of global keyword, it is only useful when you need to change the value of a variable and you don't have the scope.

Upvotes: 1

Related Questions