12

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)        
Zeynel
  • 13,145
  • 31
  • 100
  • 145
  • To change the behavior, you must change the order of your statements. Why don't you try a few different orderings of the statements to see how the behavior changes? – S.Lott Oct 22 '10 at 23:31
  • 8
    Avoid names that shadow built-ins ("list"). –  Oct 22 '10 at 23:52
  • @S.Lott: Guess and check isn't a very effective way to learn about a language. It has it's place, but I respectfully disagree in this circumstance. – JoshD Oct 23 '10 at 00:17
  • 2
    @JoshD: True. However, the question indicates such a profound lack of language skills. Guess and Check is clearly far, far better than what was presented. – S.Lott Oct 23 '10 at 03:27
  • @S.Lott: That's a good advise and I've been trying new things with the code but I still could not make it work. If you take a look at the answers; you would see that there is still no answer that appends "s" to "myList". If this is so simple; may I ask your help? Thanks. – Zeynel Oct 23 '10 at 03:53
  • @Zeynel: "that there is still no answer that appends "s" to "myList". Really? Can you provide detailed explanations of why the answers don't work? – S.Lott Oct 23 '10 at 04:14
  • 1
    -1: "I've been trying new things with the code but I still could not make it work" Delightfully vague. Could mean anything. Since no code was posted, it's impossible to understand what this means. – S.Lott Oct 23 '10 at 14:17

2 Answers2

6

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)
Community
  • 1
  • 1
dln385
  • 11,630
  • 12
  • 48
  • 58
  • Thanks. I tried this http://codepad.org/bdCaLiUs but it still rewrites the list instead of appending. – Zeynel Oct 23 '10 at 01:09
  • @Zeynel you're not following instructions – Tim McNamara Oct 23 '10 at 01:14
  • @Zeynel The code you link to looks nothing like my fix. Try copying and pasting my fix into your code. – dln385 Oct 23 '10 at 01:28
  • @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 Oct 23 '10 at 01:57
  • @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 Oct 23 '10 at 02:06
  • @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 Oct 23 '10 at 02:39
  • @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 Oct 23 '10 at 02:52
  • @dln385 Thanks. This is what is in the list for each s: s: aaa --> myList: [u'aaa'] - s: mmmm --> myList: [u'mmmm']. – Zeynel Oct 23 '10 at 03:07
  • @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 Oct 23 '10 at 04:01
  • @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 Oct 23 '10 at 04:15
  • @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 Oct 23 '10 at 04:31
  • 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 Oct 23 '10 at 04:39
  • 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 Oct 23 '10 at 04:42
5

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
  • 222,467
  • 53
  • 283
  • 367
  • +1 I think that creating `self.list` in `__init__` makes things more explicit – Tim McNamara Oct 23 '10 at 01:15
  • +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 Oct 23 '10 at 01:31
  • 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 Oct 23 '10 at 01:36
  • 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 Oct 23 '10 at 02:01
  • "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 Oct 23 '10 at 14:20