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

Grant Edwards grant.b.edwards at gmail.com
Wed Mar 6 12:59:38 EST 2024


On 2024-03-06, Roel Schroeven via Python-list <python-list at python.org> wrote:
> Op 6/03/2024 om 17:40 schreef Jacob Kruger via Python-list:
>> >>> from scoping2 import *
>
> [...]
>
> I would advice not to use 'import *', if at all possible, for multiple 
> reasons, one of which is to prevent problems like this.

Unfortunately, many (most?) tutorials for particular modules (and even
example code in the Python documentation itself) are all written
assuming that you do "from <module> import *".  It saves the tutorial
write a few keystrokes, but causes untold trouble for people who learn
incorrectly that "from <module> import *" is the proper way to do
things.

> I would also advice not to use global variables from other modules
> directly, and in fact would advice to minimize the use of globals in
> general as much as possible. If you need to keep state between
> methods, it might be better to use a class.




More information about the Python-list mailing list