An alternative to global in Python

前端 未结 4 1761
轮回少年
轮回少年 2020-12-19 05:58

I currently have code like this:

cache = 1
def foo():
    global cache
    # many
    # lines
    # of code
    cache = 2

However, this may

4条回答
  •  半阙折子戏
    2020-12-19 06:32

    "the reader may unintentionally think that the global variable has been updated" isn't much of a pitfall. You have to expect that people reading your code know how Python works. If you want to make it extra clear, use a comment. That's what they're for.

提交回复
热议问题