How should we use global variables correctly?

Joel Goldstick joel.goldstick at gmail.com
Fri Aug 23 04:22:51 EDT 2019


On Fri, Aug 23, 2019 at 4:00 AM Windson Yang <wiwindson at gmail.com> wrote:
>
> Thank you all. I agreed with Frank that
>
> > It would make sense to use the 'global' keyword if you have a module
> with various functions, several of which refer to 'foo', but only one of
> which changes the value of 'foo'.
>
> I also found an example in cpython/lib/gettext.py, only 'textdomain
> function' can change '_current_domain', other functions just refer to it.
> So, it will be not evil or to use 'global' keyword correctly when there is
> only one function can change its value?
>
> Cameron Simpson <cs at cskk.id.au> 于2019年8月23日周五 下午3:15写道:
>

The problem with deciding that only one function can change the value,
is that someone else can change the other functions.  So, if you are
working alone, it won't be a problem, but once code with globals
enters the 'wild' all bets are off.


-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays



More information about the Python-list mailing list