Sujet : Re: Variable scope inside and outside functions
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.pythonDate : 06. Mar 2024, 17:40:26
Autres entêtes
Organisation : Stefan Ram
Message-ID : <scope-20240306173836@ram.dialup.fu-berlin.de>
References : 1
Jacob Kruger <
jacob.kruger.work@gmail.com> wrote or quoted:
from scoping2 import *
Now, "dt_expiry" exists in /two/ modules: In "scoping2" and in the
current module (as a copy). To avoid this, "import scoping2" instead.
print(dt_expiry)
1970-01-01 00:00:00+00:00
do_it()
2024-03-06 18:12
This changes the variable "dt_expiry" in "scoping2", but not the
copy (also named "dt_expiry") in the current module.
print(dt_expiry)
1970-01-01 00:00:00+00:00
This prints "dt_expiry" from the current module, not from "scoping2".
To avoid this, replace "from scoping2 import *" by "import scoping2"
and replace the "dt_expiry" in the two print statements by
"scoping2.dt_expiry".