Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在init中定义变量,在get方法中设置变量,并在post方法中使用_Python_Init - Fatal编程技术网

Python 在init中定义变量,在get方法中设置变量,并在post方法中使用

Python 在init中定义变量,在get方法中设置变量,并在post方法中使用,python,init,Python,Init,我在init方法中将一个变量设置为空列表。然后,在get方法中,我查询数据库并设置列表。接下来,在我的post方法中,我尝试使用变量。但是,由于某种原因,当我的post方法运行时,变量被设置回空列表(这可能完全是意料之中的?)。你能帮我找出我做错了什么,或者让我知道是否有其他方法来做这件事吗 谢谢 class thisHandler(BaseHandler): def __init__(self, *args, **kwargs): super(thisHandler,

我在init方法中将一个变量设置为空列表。然后,在get方法中,我查询数据库并设置列表。接下来,在我的post方法中,我尝试使用变量。但是,由于某种原因,当我的post方法运行时,变量被设置回空列表(这可能完全是意料之中的?)。你能帮我找出我做错了什么,或者让我知道是否有其他方法来做这件事吗

谢谢

class thisHandler(BaseHandler):
    def __init__(self, *args, **kwargs):
        super(thisHandler, self).__init__(*args, **kwargs)
        self.topList= []

    def get(self, id):
        if self.user:
            #Other stuff happens
            self.topList = get_myList(id) #This is definitely returning a populated list as it displays just fine on my page
            #Other stuff happens
            self.render('page.html', variables)

    def post(self, id):
        #Other stuff happens
        system.debug(self.topList) #this comes back empty. this is the first reference to it in the post method
        #Other stuff happens

这些方法没有在同一执行线程中被调用。您的实例数据是线程本地的。如果希望数据从一个远程调用持续到下一个远程调用,则需要在服务器端存储会话数据,使用某种类型的cookie标识从一个调用到下一个调用的客户端会话

无论何时使用方法
get
set
,请始终坚持其含义。不要在get方法中设置/更新任何变量,也不要从set方法返回任何值。我见过很多人这样做

正确的方式:

GET method - should always return what you want
SET method - should always set the values and never return anything
话虽如此,在转到
post()
方法之前,您需要调用方法
self.get()
。这有助于更新值