How should we use global variables correctly?
Chris Angelico
rosuav at gmail.com
Thu Aug 22 20:20:49 EDT 2019
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
More information about the Python-list
mailing list