在python中引用字符串或列表全局变量

2024-04-18 18:28:34 发布

您现在位置:Python中文网/ 问答频道 /正文

在python 2.7.2+tornado 3.1.1中,如何使用全局变量?我需要它在AppEngine上工作。你知道吗

class LoginHandler(BaseHandler):
    def get(self):
        if self.get_current_user():
            self.redirect('/')
            return
        # assign self.get_argument('next', '/')) to the variable next_page
        self.render('login.html')

    def post(self):
        self.set_secure_cookie("user", self.get_argument("username"))
        # direct to next_page

Tags: toselfgetifdefpageargumenttornado
2条回答

存储next_page变量不需要全局变量。尝试使用会话或memcache。你知道吗

from google.appengine.api import memcache

# setup a key/value
memcache.set(key='next_page_%s' % user.id, next_page)

# get next_page
next_page = memcache.get(key='next_page_%s' % user.id)

我们使用'next_page_%s' % user.iduser.id作为unigue键,否则每个用户只有一个next_page。你知道吗

您需要将变量指定为每个函数中的全局变量:

global globalvariable

有关此问题的更多信息,请参见Using global variables in a function other than the one that created them。你知道吗

相关问题 更多 >