osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How should we use global variables correctly?


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