Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

Jacob Kruger jacob.kruger.work at gmail.com
Wed Mar 6 12:29:12 EST 2024


Ok, Ethan, that makes sense - I generally work with modules in folders, 
etc., but, this was just test code, but, 'see' if I instead import 
scoping2 as sc2, and then refer to sc2.dt_expiry and sc2.do_it, then it 
does operate as it should - thanks, again.


Jacob Kruger
+2782 413 4791
"Resistance is futile!...Acceptance is versatile..."


On 2024/03/06 18:57, Ethan Furman via Python-list wrote:
> On 3/6/24 08:28, Jacob Kruger via Python-list wrote:
>
> > C:\temp\py_try>python
> > Python 3.11.7 (tags/v3.11.7:fa7a6f2, Dec  4 2023, 19:24:49) [MSC 
> v.1937 64 bit (AMD64)] on win32
> > Type "help", "copyright", "credits" or "license" for more information.
> >  >>> from scoping2 import *
>
> And it becomes clear:  only do `from ... import *` when the module has 
> been specifically designed to support that.
>
> If you were to also do `import scoping2` and, after calling `do_it()`, 
> `print(scoping2.dt_expiry)`, you would see that it had changed.
>
> I know there are good explanations for how variables and names work in 
> Python, but I couldn't find any at the moment. Sorry.
>
> -- 
> ~Ethan~


More information about the Python-list mailing list