Question about creating dictionary like objects

bowman.joseph at gmail.com bowman.joseph at gmail.com
Fri Nov 6 17:06:18 EST 2009


I'm working on a memcached based session library, and I've run into an
interesting problem.

Here's the full code for the session library - http://pastebin.com/m295fdfc2

What I'm doing is using __setitem__ to set an element as a list. When
I call .append() on that list, it appears to be modifying the list in
memory, but not running __setitem__ on the parent in order for the
information to get saved in memcached.

For example, here's a test I wrote in Tornado

class TestHandler(BaseHandler):
    def get(self):
        session = sessions.Session(req_obj = self)
        if "test" in session:
            self.write("should be appending")
            session["test"].append(datetime.datetime.now())
        else:
            session["test"] = ["first"]
        self.write(str(session))

This should have just "test" in the list on first load (which it
does), the append the datetime object on every refresh, so the list
should keep growing. What I'm seeing is that the datetime does get
appended to the list in the session object that is printed, but the
__setitem__() item is never called, so it never gets updated in
memcached.

Can someone tell me what I'm doing wrong?



More information about the Python-list mailing list