Home  >  Q&A  >  body text

python flask 如何获取定义的全局变量值

如图,服务器回调到handler_access_times 函数里,定义了 g_times_map 这个全局的map变量,当客户端主动请求
get_access_times接口的时候,g_times_map 这个却始终为空怎么解决,求大神告知啊,python 这个全局变量怎么这么头疼啊

PHPzPHPz2741 days ago880

reply all(3)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:27:34

    http://stackoverflow.com/ques...
    This link has a solution, use the database to save data locally

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 10:27:34

    global variables should be defined outside the function.

    x = 1
    def f():
        global x
        print(x)
    def add():
        global x
        x += 1
    add()
    f()
    

    reply
    0
  • 黄舟

    黄舟2017-04-18 10:27:34

    If you assign a value to g_times_map outside the function, then you do not need a global statement inside the function, and you can still reference the value of g_times_map outside the function. Although I don't know what you want to do, but depending on whether you are using global g_times_map in two functions, you may also be using it in multiple functions, which is easy to make mistakes. If you just want these functions to reference the value of g_times_map outside the function, there is no need to use the global statement.

    reply
    0
  • Cancelreply