An alternative to global in Python

前端 未结 4 1754
轮回少年
轮回少年 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:40

    Using global variable is not a good programming practice. Pass the variable as an argument: make function returns something and use it in another function. Function can be assignment to variable that's how Python works.

提交回复
热议问题