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

Ethan Furman ethan at stoneleaf.us
Wed Mar 6 11:57:27 EST 2024


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