1
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()

App run fine, but there are results:

  1. localhost/login #get method >>> some value.

  2. localhost/login #get method >>> some value, some value.

  3. localhost/login #post method within form action >>> some value, some value, another value.

how is it possible ? expected result is that, after every request in test.lists will be ONLY ONE value

1 Answers1

1

You Test class defines lists as a class variable - that means the same list is shared between all instances of that class. you probably want something like this:

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

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

Now each instance will create it's own .lists attrib when it's created.

Tom Dalton
  • 6,122
  • 24
  • 35