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

How should we use global variables correctly?

Thank you all for the great explanation, I still trying to find some good
example to use 'global', In CPython, I found an example use 'global' in

    _crctable = None
    def _gen_crc(crc):
        for j in range(8):
            if crc & 1:
                crc = (crc >> 1) ^ 0xEDB88320
                crc >>= 1
        return crc

    def _ZipDecrypter(pwd):
        key0 = 305419896
        key1 = 591751049
        key2 = 878082192

        global _crctable
        if _crctable is None:
            _crctable = list(map(_gen_crc, range(256)))
        crctable = _crctable

_crctable only been used in the _ZipDecrypter function. IIUC, the code can
be refactored to

    def _gen_crc(crc):
        ...stay the same

    def _ZipDecrypter(pwd, _crctable=list(map(_gen_crc, range(256)))):
        key0 = 305419896
        key1 = 591751049
        key2 = 878082192
       crctable = _crctable

Which avoid using 'global' keyword. Why we are not doing this? I guess the
reason we use 'global' here because we don't want to  create `_crctable =
list(map(_gen_crc, range(256)))`  every time when we run '_ZipDecrypter'
function. So we kinda cache _crctable with 'global', am I right?