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 10:18 AM Cameron Simpson <cs at cskk.id.au> wrote:
> As Michael says, "you can always read from a parent scope if the name
> hasn't been used by the local scope". What this means is this:
>
>     _MODULE_LEVEL_CACHE = {}
>
>     def factors_of(n):
>         factors = _MODULE_LEVEL_CACHE.get(n)
>         if factors is None:
>             factors = factorise(n)
>             _MODULE_LEVEL_CACHE[n] = factors
>         return factors

Note that this is still using a global variable, even though it isn't
using the "global" keyword. The two concepts are related but distinct.

ChrisA