python - 误解 web.py 应用程序

标签 python instance web.py

import web

urls = ('/', 'Login')
app = web.application(urls, globals())

class Test:
    lists = []

    def bind(self, value):
        self.lists.append(value)

class Login:

     def GET(self):
         test = Test()
         test.bind('some value')
         return rest.lists

     def POST(self):
         test = Test()
         test.bind('another value')
         return test.lists


if __name__ == '__main__':
    app.run()

应用程序运行良好,但有结果:

  1. localhost/login #get 方法 >>> 一些值。

  2. localhost/login #get 方法 >>> 一些值,一些值。

  3. localhost/login #表单操作中的post方法>>>某个值,某个值,另一个值。

这怎么可能? 预期的结果是,在 test.lists 中的每个请求之后将只有一个值

最佳答案

您的Test类将列表定义为类变量 - 这意味着该类的所有实例之间共享相同的列表。你可能想要这样的东西:

class Test(object):
    def __init__(self):
        self.lists = []

    def bind(self, value):
        self.lists.append(value)

现在,每个实例在创建时都会创建自己的 .lists 属性。

关于python - 误解 web.py 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27865452/

相关文章:

python - 使用无服务器的 Numpy lambda 错误

python - 当条形分组时显示条形的值

ruby - 按属性查找对象

Java 需要帮助检查字符串是否是实例

actionscript-3 - 更改影片剪辑 Actionscript 3 的填充颜色

python - 如何在 web.py 中解码来自 github 的 POST 数据?

python - 如何使用 or-tools 在我们的 MIP 问题中设置类似 y = max(x1,x2,x3) 的等式约束?

python - 编码中的标志和深度(Pygame)

python - web.py 内存泄漏

python - 导入错误: No module named web