How to avoid computation every time a python module is reloaded

后端 未结 13 869
温柔的废话
温柔的废话 2021-02-06 10:55

I have a python module that makes use of a huge dictionary global variable, currently I put the computation code in the top section, every first time import or reload of the mod

13条回答
  •  攒了一身酷
    2021-02-06 11:26

    There's another pretty obvious solution for this problem. When code is reloaded the original scope is still available.

    So... doing something like this will make sure this code is executed only once.

    try:
        FD
    except NameError:
        FD = FreqDist(word for word in brown.words())
    

提交回复
热议问题