tags:

views:

128

answers:

2

Every time the input s comes from the form; the list is initialized again. How do I change the code to append each new s to the list?

Thank you.

class Test(webapp.RequestHandler):
    def get(self):

        s = self.request.get('sentence')
        list = []                                   
        list.append(s)                      
        htmlcode1 = HTML.table(list)        
+1  A: 

I'm not sure what the context of your code is, but this should work:

class Test(webapp.RequestHandler):
    def get(self):
        s = self.request.get('sentence')
        try:
            self.myList.append(s)
        except NameError:
            self.myList= [s]
        htmlcode1 = HTML.table(self.myList)

This makes list an instance variable so it'll stick around. The problem is that list might not exist the first time we try to use it, so in this case we need to initialize it.

Actually, looking at this post, this might be cleaner code:

class Test(webapp.RequestHandler):
    def get(self):
        s = self.request.get('sentence')
        if not hasattr(self, 'myList'):
            self.myList = []
        self.myList.append(s)
        htmlcode1 = HTML.table(self.myList)

[Edit:] The above isn't working for some reason, so try this:

class Test(webapp.RequestHandler):
    myList = []
    def get(self):
        s = self.request.get('sentence')
        self.myList.append(s)
        htmlcode1 = HTML.table(self.myList)
dln385
Thanks. I tried this http://codepad.org/bdCaLiUs but it still rewrites the list instead of appending.
Zeynel
@Zeynel you're not following instructions
Tim McNamara
@Zeynel The code you link to looks nothing like my fix. Try copying and pasting my fix into your code.
dln385
@dln385 thanks for the help. I added the exception because the way it is I get the error "htmlcode1 = HTML.table(mylist)NameError: global name 'mylist' is not defined." (I changed "list" to "mylist" as per another commenter re: not to use variables with confusing names.
Zeynel
@Zeynel I'm sorry, that was my fault entirely. The last line should have `self.list`, not `list`. I updated my examples accordingly. Also, you should use [sth's answer](http://stackoverflow.com/questions/4001652/how-to-initialize-empty-list/4001962#4001962) if you can get it to work. His code has `self.list` three times, so make sure you got all of them when you switched to `self.myList`.
dln385
@dln385 Thanks. But still s is not appended to myList. And when I try to get what inside myList I get: 'myList': myList,NameError: global name 'myList' is not defined
Zeynel
@Zeynel That error looks like you're asking for `myList` directly. `myList` is now a property of `self`, so it must be referred to as `self.myList` at all times. For example, if you want to print `myList`, you would have to use `print(self.myList)`. If I'm off the mark, could you post a larger code segment that produces that error?
dln385
@dln385 Thanks. This is what is in the list for each s: s: aaa --> myList: [u'aaa'] - s: mmmm --> myList: [u'mmmm'].
Zeynel
@Zeynel The fact that `self.myList` is being lost means that either you have a typo, or a new object is being created each time. In any case, I put up a third example to try. I've never used this strategy before, and I imagine it's bad code, but it just might work.
dln385
@dln385: Thanks!! This one worked. I appreciate your help. Any more commentary you may have about how this one works, for learning purposes, that's welcome too. Thanks again.
Zeynel
@Zeynel In the third example, I make `myList` an attribute of the __class__, not the __object__. This means that for as long as the program is run, `myList` will always remain as an attribute of the class, and you can access it from outside the class as `Test.myList`. Also, whenever you instantiate an object from the `Test` class (e.g. `testObject = Test()`), that object gets its own `myList` variable that points to the same exact data. (In other words, it's as if you had run `testObject.myList = Test.myList`.) This is why appending to `self.myList` modifies `Test.myList`.
dln385
However, `self.myList` is a different variable from `Test.myList`, so if you run `self.myList = 5`, `Test.myList` would remain unchanged. I really don't understand this well myself, but I think I should give you a few warnings. First of all, if you ever make another instance of `Test` while the program runs, that object's `myList` variable will point to the same `Test.myList`. Also, the fact that the other examples didn't work seems to show that you're creating a new object every time you want to use `get`, which is probably a bug in your code.
dln385
So what it comes down to is that I don't know whether this solution will work fine for your program, or introduce a nightmare of bugs and unexpected behavior down the road.
dln385
@dln385 Great, thank you.
Zeynel
+3  A: 

You could make the list a member variable of the object and then only update it when get() is called:

class Test(webapp.RequestHandler):
    def __init__(self, *p, **kw): # or whatever parameters this takes
        webapp.RequestHandler.__init__(self, *p, **kw)
        self.list = []

    def get(self):
        s = self.request.get('sentence')
        self.list.append(s)                      
        htmlcode1 = HTML.table(self.list)        
sth
+1 I think that creating `self.list` in `__init__` makes things more explicit
Tim McNamara
+1 This is what I wanted to do, but I gave up when I couldn't find the original `__init__()` for `webapp.RequestHandler`. I forgot about the `*p, **kw` trick.
dln385
Wait, what if `__init__` makes a call to `get`? (Again, I'm not sure what `webapp.RequestHandler` actually does). I think you should `self.list = []` before the call to `__init__`.
dln385
@sth I tried this as is (except I changed "list" to "mylist") but it still not appending "s" to the list. "mylist" is always the same as "s". Thanks for the help.
Zeynel
"but it still not appending "s" to the list"? What? How about this? Add `print` statements to show what's **actually** happening. This is the correct approach.
S.Lott