foo.py :
i = 10
def fi():
global i
i = 99
bar.py :
import foo
from foo import i
print i, foo.i
foo.fi()
print i, foo.i
This is problematic. Why does i
not change when foo.i
changes?
What Ross is saying is to restucture foo like so:
_i = 10
def getI():
return _i
def fi():
global _i
_i = 99
Then you will see it works the way you want:
>>> import foo
>>> print foo.getI()
10
>>> foo.fi()
>>> print foo.getI()
99
It is also 'better' in the sense that you avoid exporting a global, but still provide read access to it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With